Skip to content

Commit fde02ef

Browse files
committed
Ensure native create methods do not return zero (without throwing); somewhat paranoia, but also covers corner cases in which exception are canceled e.g. by DbExceptionListener
1 parent 850cb92 commit fde02ef

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStore.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ public static boolean isObjectBrowserAvailable() {
227227

228228
try {
229229
handle = nativeCreateWithFlatOptions(builder.buildFlatStoreOptions(canonicalPath), builder.model);
230+
if(handle == 0) throw new DbException("Could not create native store");
231+
230232
int debugFlags = builder.debugFlags;
231233
if (debugFlags != 0) {
232234
debugTxRead = (debugFlags & DebugFlags.LOG_TRANSACTIONS_READ) != 0;
@@ -426,6 +428,8 @@ public Transaction beginTx() {
426428
System.out.println("Begin TX with commit count " + initialCommitCount);
427429
}
428430
long nativeTx = nativeBeginTx(handle);
431+
if(nativeTx == 0) throw new DbException("Could not create native transaction");
432+
429433
Transaction tx = new Transaction(this, nativeTx, initialCommitCount);
430434
synchronized (transactions) {
431435
transactions.add(tx);
@@ -450,6 +454,8 @@ public Transaction beginReadTx() {
450454
System.out.println("Begin read TX with commit count " + initialCommitCount);
451455
}
452456
long nativeTx = nativeBeginReadTx(handle);
457+
if(nativeTx == 0) throw new DbException("Could not create native read transaction");
458+
453459
Transaction tx = new Transaction(this, nativeTx, initialCommitCount);
454460
synchronized (transactions) {
455461
transactions.add(tx);

objectbox-java/src/main/java/io/objectbox/Transaction.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import io.objectbox.annotation.apihint.Experimental;
2424
import io.objectbox.annotation.apihint.Internal;
25+
import io.objectbox.exception.DbException;
2526
import io.objectbox.internal.CursorFactory;
2627

2728
@Internal
@@ -183,6 +184,7 @@ public <T> Cursor<T> createCursor(Class<T> entityClass) {
183184
EntityInfo<T> entityInfo = store.getEntityInfo(entityClass);
184185
CursorFactory<T> factory = entityInfo.getCursorFactory();
185186
long cursorHandle = nativeCreateCursor(transaction, entityInfo.getDbName(), entityClass);
187+
if(cursorHandle == 0) throw new DbException("Could not create native cursor");
186188
return factory.createCursor(this, cursorHandle, store);
187189
}
188190

objectbox-java/src/main/java/io/objectbox/internal/DebugCursor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.objectbox.InternalAccess;
2222
import io.objectbox.Transaction;
2323
import io.objectbox.annotation.apihint.Beta;
24+
import io.objectbox.exception.DbException;
2425

2526
/** Not intended for normal use. */
2627
@Beta
@@ -40,7 +41,9 @@ public class DebugCursor implements Closeable {
4041

4142
public static DebugCursor create(Transaction tx) {
4243
long txHandle = InternalAccess.getHandle(tx);
43-
return new DebugCursor(tx, nativeCreate(txHandle));
44+
long handle = nativeCreate(txHandle);
45+
if(handle == 0) throw new DbException("Could not create native debug cursor");
46+
return new DebugCursor(tx, handle);
4447
}
4548

4649
public DebugCursor(Transaction tx, long handle) {

objectbox-java/src/main/java/io/objectbox/query/QueryBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.objectbox.Property;
3030
import io.objectbox.annotation.apihint.Experimental;
3131
import io.objectbox.annotation.apihint.Internal;
32+
import io.objectbox.exception.DbException;
3233
import io.objectbox.relation.RelationInfo;
3334

3435
/**
@@ -191,6 +192,7 @@ public QueryBuilder(Box<T> box, long storeHandle, String entityName) {
191192
this.box = box;
192193
this.storeHandle = storeHandle;
193194
handle = nativeCreate(storeHandle, entityName);
195+
if(handle == 0) throw new DbException("Could not create native query builder");
194196
isSubQuery = false;
195197
}
196198

@@ -232,6 +234,7 @@ public Query<T> build() {
232234
throw new IllegalStateException("Incomplete logic condition. Use or()/and() between two conditions only.");
233235
}
234236
long queryHandle = nativeBuild(handle);
237+
if(queryHandle == 0) throw new DbException("Could not create native query");
235238
Query<T> query = new Query<>(box, queryHandle, eagerRelations, filter, comparator);
236239
close();
237240
return query;

0 commit comments

Comments
 (0)