#endif
/* Helpers for jstring and jbyteArray. */
-static const char * s3jni__jstring_to_mutf8_bytes(JNIEnv * const env, jstring v ){
+static const char * s3jni__jstring_to_mutf8(JNIEnv * const env, jstring v ){
const char *z = v ? (*env)->GetStringUTFChars(env, v, NULL) : 0;
s3jni_oom_check( v ? !!z : !z );
return z;
}
-#define s3jni_jstring_to_mutf8(ARG) s3jni__jstring_to_mutf8_bytes(env, (ARG))
+#define s3jni_jstring_to_mutf8(ARG) s3jni__jstring_to_mutf8(env, (ARG))
#define s3jni_mutf8_release(ARG,VAR) if( VAR ) (*env)->ReleaseStringUTFChars(env, ARG, VAR)
static jbyte * s3jni__jbyteArray_bytes(JNIEnv * const env, jbyteArray jBA ){
*/
#define S3JniDb_from_c(sqlite3Ptr) \
((sqlite3Ptr) ? S3JniDb_from_clientdata(sqlite3Ptr) : 0)
+#define S3JniDb_from_jlong(sqlite3PtrAsLong) \
+ S3JniDb_from_c(S3JniLongPtr_T(sqlite3,sqlite3PtrAsLong))
/*
** Unref any Java-side state in (S3JniAutoExtension*) AX and zero out
S3JniApi(sqlite3_busy_handler(),jint,1busy_1handler)(
JniArgsEnvClass, jlong jpDb, jobject jBusy
){
- S3JniDb * const ps = S3JniDb_from_c(S3JniLongPtr_sqlite3(jpDb));
+ S3JniDb * const ps = S3JniDb_from_jlong(jpDb);
S3JniHook * const pHook = ps ? &ps->hooks.busyHandler : 0;
S3JniHook hook = S3JniHook_empty;
int rc = 0;
S3JniApi(sqlite3_busy_timeout(),jint,1busy_1timeout)(
JniArgsEnvClass, jlong jpDb, jint ms
){
- S3JniDb * const ps = S3JniDb_from_c(S3JniLongPtr_sqlite3(jpDb));
+ S3JniDb * const ps = S3JniDb_from_jlong(jpDb);
int rc = SQLITE_MISUSE;
if( ps ){
S3JniDb_mutex_enter;
/* Wrapper for sqlite3_close(_v2)(). */
static jint s3jni_close_db(JNIEnv * const env, jlong jpDb, int version){
int rc = 0;
- S3JniDb * const ps = S3JniDb_from_c(S3JniLongPtr_sqlite3(jpDb));
+ S3JniDb * const ps = S3JniDb_from_jlong(jpDb);
assert(version == 1 || version == 2);
if( ps ){
}
S3JniApi(sqlite3_collation_needed(),jint,1collation_1needed)(
- JniArgsEnvClass, jobject jDb, jobject jHook
+ JniArgsEnvClass, jlong jpDb, jobject jHook
){
S3JniDb * ps;
S3JniCollationNeeded * pHook;
int rc = 0;
S3JniDb_mutex_enter;
- ps = S3JniDb_from_java(jDb);
+ ps = S3JniDb_from_jlong(jpDb);
if( !ps ){
S3JniDb_mutex_leave;
return SQLITE_MISUSE;
** sqlite3_rollback_hook().
*/
static jobject s3jni_commit_rollback_hook(int isCommit, JNIEnv * const env,
- jobject jDb, jobject jHook){
+ jlong jpDb, jobject jHook){
S3JniDb * ps;
jobject pOld = 0; /* previous hoook */
S3JniHook * pHook; /* ps->hooks.commit|rollback */
S3JniDb_mutex_enter;
- ps = S3JniDb_from_java(jDb);
+ ps = S3JniDb_from_jlong(jpDb);
if( !ps ){
s3jni_db_error(ps->pDb, SQLITE_NOMEM, 0);
S3JniDb_mutex_leave;
}
S3JniApi(sqlite3_commit_hook(),jobject,1commit_1hook)(
- JniArgsEnvClass,jobject jDb, jobject jHook
+ JniArgsEnvClass, jlong jpDb, jobject jHook
){
- return s3jni_commit_rollback_hook(1, env, jDb, jHook);
+ return s3jni_commit_rollback_hook(1, env, jpDb, jHook);
}
S3JniApi(sqlite3_compileoption_get(),jstring,1compileoption_1get)(
S3JniApi(sqlite3_compileoption_used(),jboolean,1compileoption_1used)(
JniArgsEnvClass, jstring name
){
- const char *zUtf8 = s3jni_jstring_to_mutf8(name)
- /* We know these to be ASCII, so MUTF-8 is fine. */;
- const jboolean rc =
- 0==sqlite3_compileoption_used(zUtf8) ? JNI_FALSE : JNI_TRUE;
- s3jni_mutf8_release(name, zUtf8);
- return rc;
+ if( name ){
+ const char *zUtf8 = s3jni_jstring_to_mutf8(name)
+ /* We know these to be ASCII, so MUTF-8 is fine (and
+ hypothetically faster to convert). */;
+ const jboolean rc =
+ 0==sqlite3_compileoption_used(zUtf8) ? JNI_FALSE : JNI_TRUE;
+ s3jni_mutf8_release(name, zUtf8);
+ return rc;
+ }else{
+ return JNI_FALSE;
+ }
}
S3JniApi(sqlite3_config() /*for a small subset of options.*/,
S3JniDb_mutex_enter;
ps = S3JniDb_from_java(jDb);
- if( !ps ){
+ if( !ps || !name ){
rc = SQLITE_MISUSE;
}else{
jclass const klazz = (*env)->GetObjectClass(env, oCollation);
sqlite3 * const pDb = PtrGet_sqlite3(jDb);
char * zFuncName = 0;
- if( !encodingTypeIsValid(eTextRep) ){
+ if( !pDb || !jFuncName ){
+ return SQLITE_MISUSE;
+ }else if( !encodingTypeIsValid(eTextRep) ){
return s3jni_db_error(pDb, SQLITE_FORMAT,
"Invalid function encoding option.");
}
** sqlite3_preupdate_hook() (if isPre is true).
*/
static jobject s3jni_updatepre_hook(JNIEnv * env, int isPre, jlong jpDb, jobject jHook){
- S3JniDb * const ps = S3JniDb_from_c(S3JniLongPtr_sqlite3(jpDb));
+ S3JniDb * const ps = S3JniDb_from_jlong(jpDb);
jclass klazz;
jobject pOld = 0;
jmethodID xCallback;
}
S3JniApi(sqlite3_rollback_hook(),jobject,1rollback_1hook)(
- JniArgsEnvClass, jobject jDb, jobject jHook
+ JniArgsEnvClass, jlong jpDb, jobject jHook
){
- return s3jni_commit_rollback_hook(0, env, jDb, jHook);
+ return s3jni_commit_rollback_hook(0, env, jpDb, jHook);
}
/* Callback for sqlite3_set_authorizer(). */
/*
* Class: org_sqlite_jni_SQLite3Jni
* Method: sqlite3_collation_needed
- * Signature: (Lorg/sqlite/jni/sqlite3;Lorg/sqlite/jni/CollationNeededCallback;)I
+ * Signature: (JLorg/sqlite/jni/CollationNeededCallback;)I
*/
JNIEXPORT jint JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1collation_1needed
- (JNIEnv *, jclass, jobject, jobject);
+ (JNIEnv *, jclass, jlong, jobject);
/*
* Class: org_sqlite_jni_SQLite3Jni
* Method: sqlite3_commit_hook
- * Signature: (Lorg/sqlite/jni/sqlite3;Lorg/sqlite/jni/CommitHookCallback;)Lorg/sqlite/jni/CommitHookCallback;
+ * Signature: (JLorg/sqlite/jni/CommitHookCallback;)Lorg/sqlite/jni/CommitHookCallback;
*/
JNIEXPORT jobject JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1commit_1hook
- (JNIEnv *, jclass, jobject, jobject);
+ (JNIEnv *, jclass, jlong, jobject);
/*
* Class: org_sqlite_jni_SQLite3Jni
/*
* Class: org_sqlite_jni_SQLite3Jni
* Method: sqlite3_rollback_hook
- * Signature: (Lorg/sqlite/jni/sqlite3;Lorg/sqlite/jni/RollbackHookCallback;)Lorg/sqlite/jni/RollbackHookCallback;
+ * Signature: (JLorg/sqlite/jni/RollbackHookCallback;)Lorg/sqlite/jni/RollbackHookCallback;
*/
JNIEXPORT jobject JNICALL Java_org_sqlite_jni_SQLite3Jni_sqlite3_1rollback_1hook
- (JNIEnv *, jclass, jobject, jobject);
+ (JNIEnv *, jclass, jlong, jobject);
/*
* Class: org_sqlite_jni_SQLite3Jni
//! Called from static init code.
private static native void init();
+ /**
+ Returns a nul-terminated copy of s as a byte array, or null if s
+ is null.
+ */
+ private static byte[] nulTerminateUtf8(String s){
+ return null==s ? null : (s+"\0").getBytes(StandardCharsets.UTF_8);
+ }
+
/**
Each thread which uses the SQLite3 JNI APIs should call
sqlite3_jni_uncache_thread() when it is done with the library -
public static int sqlite3_bind_parameter_index(
@NotNull sqlite3_stmt stmt, @NotNull String paramName
){
- final byte[] utf8 = (paramName+"\0").getBytes(StandardCharsets.UTF_8);
- return sqlite3_bind_parameter_index(stmt.getNativePointer(), utf8);
+ final byte[] utf8 = nulTerminateUtf8(paramName);
+ return null==utf8 ? 0 : sqlite3_bind_parameter_index(stmt.getNativePointer(), utf8);
}
@Canonical
@NotNull sqlite3_stmt stmt, int ndx
);
+ @Canonical
+ static native int sqlite3_collation_needed(
+ @NotNull long ptrToDb, @Nullable CollationNeededCallback callback
+ );
+
/**
This functions like C's sqlite3_collation_needed16() because
Java's string type is inherently compatible with that interface.
*/
@Canonical
- public static native int sqlite3_collation_needed(
+ public static int sqlite3_collation_needed(
@NotNull sqlite3 db, @Nullable CollationNeededCallback callback
+ ){
+ return sqlite3_collation_needed(db.getNativePointer(), callback);
+ }
+
+ @Canonical
+ static native CommitHookCallback sqlite3_commit_hook(
+ @NotNull long ptrToDb, @Nullable CommitHookCallback hook
);
@Canonical
- public static native CommitHookCallback sqlite3_commit_hook(
+ public static CommitHookCallback sqlite3_commit_hook(
@NotNull sqlite3 db, @Nullable CommitHookCallback hook
- );
+ ){
+ return sqlite3_commit_hook(db.getNativePointer(), hook);
+ }
@Canonical
- public static native String sqlite3_compileoption_get(
- int n
- );
+ public static native String sqlite3_compileoption_get(int n);
+ /**
+ Unlike the C API, returns false if its argument is NULL (as
+ opposed to invoking UB).
+ */
@Canonical
- public static native boolean sqlite3_compileoption_used(
- @NotNull String optName
- );
+ public static native boolean sqlite3_compileoption_used(String optName);
/**
This implementation is private because it's too easy to pass it
@NotNull byte[] nulTerminatedUtf8Sql
);
+ /**
+ Unlike this C API, this returns SQLITE_MISUSE if its argument is
+ null (as opposed to invoking UB).
+ */
@Canonical()
- public static int sqlite3_complete(@NotNull String sql){
- /* Design note: we don't implement this in native code because we
- won't get a NUL-terminated string there unless we make our own
- copy to add a terminator. That's much easier to do here. */
- return sqlite3_complete( (sql+"\0").getBytes(StandardCharsets.UTF_8) );
+ public static int sqlite3_complete(String sql){
+ return null==sql ? SQLITE_MISUSE : sqlite3_complete( nulTerminateUtf8(sql) );
}
@Canonical(comment="Option subset: SQLITE_CONFIG_LOG")
public static native int sqlite3_config( @Nullable ConfigLogCallback logger );
+ /**
+ Unlike this C API, this returns null if its argument is
+ null (as opposed to invoking UB).
+ */
@Canonical
public static native sqlite3 sqlite3_context_db_handle(
@NotNull sqlite3_context cx
);
+ /**
+ Unlike this C API, this returns SQLITE_MISUSE if its db or name
+ arguments are null (as opposed to invoking UB).
+ */
@Canonical
public static native int sqlite3_create_collation(
@NotNull sqlite3 db, @NotNull String name, int eTextRep,
depends on which methods the final argument implements. See
SQLFunction's subclasses (ScalarFunction, AggregateFunction<T>,
and WindowFunction<T>) for details.
+
+ Unlike this C API, this returns SQLITE_MISUSE null if its db or
+ functionName arguments are null (as opposed to invoking UB).
*/
@Canonical
public static native int sqlite3_create_function(
Overload for sqlite3_db_config() calls which take (int,int*)
variadic arguments. Returns SQLITE_MISUSE if op is not one of the
SQLITE_DBCONFIG_... options which uses this call form.
+
+ Unlike this C API, this returns SQLITE_MISUSE if its db argument
+ are null (as opposed to invoking UB).
*/
@Canonical
public static native int sqlite3_db_config(
}
@Canonical
- public static native RollbackHookCallback sqlite3_rollback_hook(
- @NotNull sqlite3 db, @Nullable RollbackHookCallback hook
+ static native RollbackHookCallback sqlite3_rollback_hook(
+ @NotNull long ptrToDb, @Nullable RollbackHookCallback hook
);
+ @Canonical
+ public static RollbackHookCallback sqlite3_rollback_hook(
+ @NotNull sqlite3 db, @Nullable RollbackHookCallback hook
+ ){
+ return sqlite3_rollback_hook(db.getNativePointer(), hook);
+ }
+
@Canonical
public static native int sqlite3_set_authorizer(
@NotNull sqlite3 db, @Nullable AuthorizerCallback auth
public static int sqlite3_strglob(
@NotNull String glob, @NotNull String txt
){
- return sqlite3_strglob(
- (glob+"\0").getBytes(StandardCharsets.UTF_8),
- (txt+"\0").getBytes(StandardCharsets.UTF_8)
- );
+ return sqlite3_strglob(nulTerminateUtf8(glob),
+ nulTerminateUtf8(txt));
}
/**
public static int sqlite3_strlike(
@NotNull String glob, @NotNull String txt, char escChar
){
- return sqlite3_strlike(
- (glob+"\0").getBytes(StandardCharsets.UTF_8),
- (txt+"\0").getBytes(StandardCharsets.UTF_8),
- (int)escChar
- );
+ return sqlite3_strlike(nulTerminateUtf8(glob),
+ nulTerminateUtf8(txt),
+ (int)escChar);
}
@Canonical
-C JNI:\scorrect\sa\sNullPointerException\striggered\svia\sSQLTester.
-D 2023-09-28T20:34:28.893
+C More\swork\stowards\sthe\snew\spointer-passing\smechanism\sin\sJNI,\sand\scode-adjacent\scleanups.
+D 2023-09-30T09:41:58.693
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
F ext/jni/GNUmakefile 42e00052401b6dd41c0cdd53b31450606ea37486283abdb038dff9be74bff71e
F ext/jni/README.md 9fceaeb17cecdc5d699dfc83c0cbc3a03fdb3b86bf676381894166c73375ee75
F ext/jni/jar-dist.make 030aaa4ae71dd86e4ec5e7c1e6cd86f9dfa47c4592c070d2e35157e42498e1fa
-F ext/jni/src/c/sqlite3-jni.c 9d5ea1bf8c7da874fdd6dc3f7cd05632c82e747fb6f4b4b3967f7aadae3ac910
-F ext/jni/src/c/sqlite3-jni.h 0c13ef53143ad6b3ed0e6b15f706f9f03e9c0c6c4f896a5846e4a68e58da83c8
+F ext/jni/src/c/sqlite3-jni.c caab9e9fdb0b8d8682c730d9bbc166778971e86f443c3f6b57e70aca86236f0c
+F ext/jni/src/c/sqlite3-jni.h e3ec460570ef74f1f3d7725f93a8cf89840e1fee983741a7939c5dc992971df5
F ext/jni/src/org/sqlite/jni/AbstractCollationCallback.java 95e88ba04f4aac51ffec65693e878e234088b2f21b387f4e4285c8b72b33e436
F ext/jni/src/org/sqlite/jni/AggregateFunction.java 7312486bc65fecdb91753c0a4515799194e031f45edbe16a6373cea18f404dc4
F ext/jni/src/org/sqlite/jni/AuthorizerCallback.java e6135be32f12bf140bffa39be7fd1a45ad83b2661ed49c08dbde04c8485feb38
F ext/jni/src/org/sqlite/jni/ResultCode.java ba701f20213a5f259e94cfbfdd36eb7ac7ce7797f2c6c7fca2004ff12ce20f86
F ext/jni/src/org/sqlite/jni/RollbackHookCallback.java d12352c0e22840de484ffa9b11ed5058bb0daca2e9f218055d3c54c947a273c4
F ext/jni/src/org/sqlite/jni/SQLFunction.java 544a875d33fd160467d82e2397ac33157b29971d715a821a4fad3c899113ee8c
-F ext/jni/src/org/sqlite/jni/SQLite3Jni.java bb7436bcac55480ae5c5ba62fb92861ea0e948c03bc1475ef202aa26a819ed61
+F ext/jni/src/org/sqlite/jni/SQLite3Jni.java 2cce647774f7f8511c3b61cd280c19d871b49c401155e3f17c346db5d05ac824
F ext/jni/src/org/sqlite/jni/ScalarFunction.java 6d387bb499fbe3bc13c53315335233dbf6a0c711e8fa7c521683219b041c614c
F ext/jni/src/org/sqlite/jni/TableColumnMetadata.java 54511b4297fa28dcb3f49b24035e34ced10e3fd44fd0e458e784f4d6b0096dab
F ext/jni/src/org/sqlite/jni/Tester1.java 720e1efddd769d5785e95100ff48aa203f2288eea865326a1a81fd5af43ec3a5
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P 4182f0275d5d65e04a130eeef4d44642a5ffeeb4b84430d240ea2605345f1404
-R a2d8547ef3a0aabe5f166da28f6b427d
+P 0a873de76c0cbcd8e2eda3f0508e427f1dcb32b01798687c0545acfe10102179
+R 85f58d35114ff163d0c32151ce0233f3
U stephan
-Z 0bc34c11fb15e6b9fa15ab1d2bacfac5
+Z 80cc94af82e9bdc221a901efb80be260
# Remove this line to create a well-formed Fossil manifest.
-0a873de76c0cbcd8e2eda3f0508e427f1dcb32b01798687c0545acfe10102179
\ No newline at end of file
+6c63987e893357dc8b10decaa96c30fb37b75481640a303e77a0d8224354491e
\ No newline at end of file