Add missing JNI bindings for sqlite3_db_readonly() and sqlite3_db_name(). Code-adjacent tweaks for the API_ARMOR audit.

FossilOrigin-Name: e5eace747ae656f1e05a346e53a2827242e57107567d2c3470e292cf0b40858d
This commit is contained in:
stephan 2023-10-14 12:19:59 +00:00
parent 7dc0cc45f3
commit 2ba1acce81
6 changed files with 98 additions and 45 deletions

View File

@ -909,7 +909,7 @@ static S3JniEnv * S3JniEnv__get(JNIEnv * const env){
**
** For purposes of certain hand-crafted JNI function bindings, we
** need a way of reporting errors which is consistent with the rest of
** the C API, as opposed to throwing JS exceptions. To that end, this
** the C API, as opposed to throwing Java exceptions. To that end, this
** internal-use-only function is a thin proxy around
** sqlite3ErrorWithMessage(). The intent is that it only be used from
** JNI bindings such as sqlite3_prepare_v2/v3(), and definitely not
@ -2057,6 +2057,13 @@ static void udf_xInverse(sqlite3_context* cx, int argc,
JniDecl(jlong,JniNameSuffix)(JniArgsEnvClass, jlong jpDb){ \
return (jlong)CName(S3JniLongPtr_sqlite3(jpDb)); \
}
/** Create a trivial JNI wrapper for (jstring CName(sqlite3*,int)). */
#define WRAP_STR_DB_INT(JniNameSuffix,CName) \
JniDecl(jstring,JniNameSuffix)(JniArgsEnvClass, jlong jpDb, jint ndx){ \
return s3jni_utf8_to_jstring( \
CName(S3JniLongPtr_sqlite3(jpDb), (int)ndx), \
-1); \
}
/** Create a trivial JNI wrapper for (int CName(sqlite3_value*)). */
#define WRAP_INT_SVALUE(JniNameSuffix,CName) \
JniDecl(jint,JniNameSuffix)(JniArgsEnvClass, jlong jpSValue){ \
@ -2082,6 +2089,7 @@ WRAP_STR_STMT_INT(1column_1origin_1name, sqlite3_column_origin_name)
WRAP_STR_STMT_INT(1column_1table_1name, sqlite3_column_table_name)
WRAP_INT_STMT_INT(1column_1type, sqlite3_column_type)
WRAP_INT_STMT(1data_1count, sqlite3_data_count)
WRAP_STR_DB_INT(1db_1name, sqlite3_db_name)
WRAP_INT_DB(1error_1offset, sqlite3_error_offset)
WRAP_INT_DB(1extended_1errcode, sqlite3_extended_errcode)
WRAP_BOOL_DB(1get_1autocommit, sqlite3_get_autocommit)
@ -2125,6 +2133,7 @@ WRAP_INT_SVALUE(1value_1type, sqlite3_value_type)
#undef WRAP_INT_VOID
#undef WRAP_MUTF8_VOID
#undef WRAP_STR_STMT_INT
#undef WRAP_STR_DB_INT
S3JniApi(sqlite3_aggregate_context(),jlong,1aggregate_1context)(
JniArgsEnvClass, jobject jCx, jboolean initialize
@ -2866,7 +2875,7 @@ static jobject s3jni_commit_rollback_hook(int isCommit, JNIEnv * const env,
S3JniDb_mutex_enter;
ps = S3JniDb_from_jlong(jpDb);
if( !ps ){
s3jni_db_error(ps->pDb, SQLITE_NOMEM, 0);
s3jni_db_error(ps->pDb, SQLITE_MISUSE, 0);
S3JniDb_mutex_leave;
return 0;
}
@ -3252,36 +3261,6 @@ error_cleanup:
return (jint)rc;
}
S3JniApi(sqlite3_db_filename(),jstring,1db_1filename)(
JniArgsEnvClass, jobject jDb, jstring jDbName
){
S3JniDb * const ps = S3JniDb_from_java(jDb);
char *zDbName;
jstring jRv = 0;
int nStr = 0;
if( !ps || !jDbName ){
return 0;
}
zDbName = s3jni_jstring_to_utf8( jDbName, &nStr);
if( zDbName ){
char const * zRv = sqlite3_db_filename(ps->pDb, zDbName);
sqlite3_free(zDbName);
if( zRv ){
jRv = s3jni_utf8_to_jstring( zRv, -1);
}
}
return jRv;
}
S3JniApi(sqlite3_db_handle(),jobject,1db_1handle)(
JniArgsEnvClass, jobject jpStmt
){
sqlite3_stmt * const pStmt = PtrGet_sqlite3_stmt(jpStmt);
sqlite3 * const pDb = pStmt ? sqlite3_db_handle(pStmt) : 0;
S3JniDb * const ps = pDb ? S3JniDb_from_c(pDb) : 0;
return ps ? ps->jDb : 0;
}
S3JniApi(sqlite3_db_config() /*for MAINDBNAME*/,
jint,1db_1config__Lorg_sqlite_jni_sqlite3_2ILjava_lang_String_2
@ -3309,6 +3288,7 @@ S3JniApi(sqlite3_db_config() /*for MAINDBNAME*/,
}
S3JniDb_mutex_leave;
break;
case 0:
default:
rc = SQLITE_MISUSE;
}
@ -3352,6 +3332,7 @@ S3JniApi(
}
break;
}
case 0:
default:
rc = SQLITE_MISUSE;
}
@ -3372,6 +3353,48 @@ JniDecl(jint,1db_1config__Lorg_sqlite_jni_sqlite3_2IILorg_sqlite_jni_OutputPoint
);
}
S3JniApi(sqlite3_db_filename(),jstring,1db_1filename)(
JniArgsEnvClass, jobject jDb, jstring jDbName
){
S3JniDb * const ps = S3JniDb_from_java(jDb);
char *zDbName;
jstring jRv = 0;
int nStr = 0;
if( !ps || !jDbName ){
return 0;
}
zDbName = s3jni_jstring_to_utf8( jDbName, &nStr);
if( zDbName ){
char const * zRv = sqlite3_db_filename(ps->pDb, zDbName);
sqlite3_free(zDbName);
if( zRv ){
jRv = s3jni_utf8_to_jstring( zRv, -1);
}
}
return jRv;
}
S3JniApi(sqlite3_db_handle(),jobject,1db_1handle)(
JniArgsEnvClass, jobject jpStmt
){
sqlite3_stmt * const pStmt = PtrGet_sqlite3_stmt(jpStmt);
sqlite3 * const pDb = pStmt ? sqlite3_db_handle(pStmt) : 0;
S3JniDb * const ps = pDb ? S3JniDb_from_c(pDb) : 0;
return ps ? ps->jDb : 0;
}
S3JniApi(sqlite3_db_readonly(),jint,1db_1readonly)(
JniArgsEnvClass, jobject jDb, jstring jDbName
){
int rc = 0;
S3JniDb * const ps = S3JniDb_from_java(jDb);
char *zDbName = jDbName ? s3jni_jstring_to_utf8( jDbName, 0 ) : 0;
rc = sqlite3_db_readonly(ps ? ps->pDb : 0, zDbName);
sqlite3_free(zDbName);
return (jint)rc;
}
S3JniApi(sqlite3_db_release_memory(),int,1db_1release_1memory)(
JniArgsEnvClass, jobject jDb
){

View File

@ -1307,6 +1307,14 @@ JNIEXPORT jint JNICALL Java_org_sqlite_jni_CApi_sqlite3_1db_1config__Lorg_sqlite
JNIEXPORT jint JNICALL Java_org_sqlite_jni_CApi_sqlite3_1db_1config__Lorg_sqlite_jni_sqlite3_2ILjava_lang_String_2
(JNIEnv *, jclass, jobject, jint, jstring);
/*
* Class: org_sqlite_jni_CApi
* Method: sqlite3_db_name
* Signature: (JI)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_org_sqlite_jni_CApi_sqlite3_1db_1name
(JNIEnv *, jclass, jlong, jint);
/*
* Class: org_sqlite_jni_CApi
* Method: sqlite3_db_filename
@ -1323,6 +1331,14 @@ JNIEXPORT jstring JNICALL Java_org_sqlite_jni_CApi_sqlite3_1db_1filename
JNIEXPORT jobject JNICALL Java_org_sqlite_jni_CApi_sqlite3_1db_1handle
(JNIEnv *, jclass, jobject);
/*
* Class: org_sqlite_jni_CApi
* Method: sqlite3_db_readonly
* Signature: (Lorg/sqlite/jni/sqlite3;Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_org_sqlite_jni_CApi_sqlite3_1db_1readonly
(JNIEnv *, jclass, jobject, jstring);
/*
* Class: org_sqlite_jni_CApi
* Method: sqlite3_db_release_memory

View File

@ -811,12 +811,21 @@ final class CApi {
@NotNull sqlite3 db, int op, @NotNull String val
);
private static native String sqlite3_db_name(@NotNull long ptrToDb, int ndx);
public static String sqlite3_db_name(@NotNull sqlite3 db, int ndx){
return null==db ? null : sqlite3_db_name(db.getNativePointer(), ndx);
}
public static native String sqlite3_db_filename(
@NotNull sqlite3 db, @NotNull String dbName
);
public static native sqlite3 sqlite3_db_handle(@NotNull sqlite3_stmt stmt);
public static native int sqlite3_db_readonly(@NotNull sqlite3 db, String dbName);
public static native int sqlite3_db_release_memory(sqlite3 db);
public static native int sqlite3_db_status(

View File

@ -255,6 +255,11 @@ public class Tester1 implements Runnable {
and this call is here to ensure that the build fails
if it cannot find both names. */;
affirm( 0==sqlite3_db_readonly(db,"main") );
affirm( 0==sqlite3_db_readonly(db,null) );
affirm( 0>sqlite3_db_readonly(db,"nope") );
affirm( 0>sqlite3_db_readonly(null,null) );
// These interrupt checks are only to make sure that the JNI binding
// has the proper exported symbol names. They don't actually test
// anything useful.
@ -1028,8 +1033,11 @@ public class Tester1 implements Runnable {
affirm( 0 == rc );
affirm( outDb.get() != db1 );
final sqlite3 db2 = outDb.get();
affirm( "main".equals( sqlite3_db_name(db1, 0) ) );
rc = sqlite3_db_config(db1, SQLITE_DBCONFIG_MAINDBNAME, "foo");
affirm( sqlite3_db_filename(db1, "foo").endsWith(dbName) );
affirm( "foo".equals( sqlite3_db_name(db1, 0) ) );
final ValueHolder<Integer> xBusyCalled = new ValueHolder<>(0);
BusyHandlerCallback handler = new BusyHandlerCallback(){

View File

@ -1,5 +1,5 @@
C Round\sone\sof\san\saudit\sfor\sSQLITE_ENABLE_API_ARMOR\sfor\sfunctions\sexposed\sby\sJNI\sand\sthose\sfunctions\smissing\sarmor,\sas\s[forum:5e3fc453a69b49ca|reported\sin\sseveral\sforum\sposts].
D 2023-10-13T12:48:35.298
C Add\smissing\sJNI\sbindings\sfor\ssqlite3_db_readonly()\sand\ssqlite3_db_name().\sCode-adjacent\stweaks\sfor\sthe\sAPI_ARMOR\saudit.
D 2023-10-14T12:19:59.876
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -238,14 +238,14 @@ F ext/icu/sqliteicu.h fa373836ed5a1ee7478bdf8a1650689294e41d0c89c1daab26e9ae78a3
F ext/jni/GNUmakefile 6da240c9a11701f3ed569384cd15ef611e8b3c5e3897d265923b14bf0e1eb272
F ext/jni/README.md ef9ac115e97704ea995d743b4a8334e23c659e5534c3b64065a5405256d5f2f4
F ext/jni/jar-dist.make 030aaa4ae71dd86e4ec5e7c1e6cd86f9dfa47c4592c070d2e35157e42498e1fa
F ext/jni/src/c/sqlite3-jni.c eabd48ced29954fb4f6173c4f350f7f1fce218ce19f48a75367cb560cf2da77e
F ext/jni/src/c/sqlite3-jni.h be1fdff7ab3a2bb357197271c8ac5d2bf6ff59380c106dde3a13be88724bad22
F ext/jni/src/c/sqlite3-jni.c 2a01d42df24718d69eafe836643de272b14fca57135ce0f1f6521fecf9002e29
F ext/jni/src/c/sqlite3-jni.h 8bd34a6fe3b51059690be31ac5bd6e454a738dd1ff9916f490e05cf63c4f8e37
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 fde5f758ad170ca45ae00b12194c8ba8d8f3090bd64cc3e002dd9c5e7dff8568
F ext/jni/src/org/sqlite/jni/AutoExtensionCallback.java c0fbfd3779fc92982c7935325a7484dee43eeb80d716989ed31218f453addb94
F ext/jni/src/org/sqlite/jni/BusyHandlerCallback.java 4cb7fc70efd55583fed6033c34a8719da42975ca97ef4781dda0b9f6cc8ec2e8
F ext/jni/src/org/sqlite/jni/CApi.java c1dde485a3a3f43c46c8d9c527f9ba5bf303fe0409b2c0de253fb7b6e1055f7e
F ext/jni/src/org/sqlite/jni/CApi.java 6f878d291642568a06d9a36c15b420f5f867dcc44cee0cc56e8f02b542f67ec4
F ext/jni/src/org/sqlite/jni/CallbackProxy.java 064a8a00e4c63cc501c30504f93ca996d422c5f010067f969b2d0a10f0868153
F ext/jni/src/org/sqlite/jni/CollationCallback.java 8cf57cb014a645ecc12609eed17308852a597bc5e83d82a4fdb90f7fadc25f9d
F ext/jni/src/org/sqlite/jni/CollationNeededCallback.java 0c62245e000d5db52576c728cac20f6a31f31f5cf40ca4cbcd64b22964e82ae5
@ -265,7 +265,7 @@ F ext/jni/src/org/sqlite/jni/ScalarFunction.java 6d387bb499fbe3bc13c53315335233d
F ext/jni/src/org/sqlite/jni/Sqlite.java 1617ea2bf3dfa493b7f031a3187cbfd6837c39bc1d406c4b3edcf9aab941639d
F ext/jni/src/org/sqlite/jni/SqliteException.java e17500e8bca2c68c260d8d0163fe4b7dc8bd0b1b90211201325c4a5566ce75ca
F ext/jni/src/org/sqlite/jni/TableColumnMetadata.java 54511b4297fa28dcb3f49b24035e34ced10e3fd44fd0e458e784f4d6b0096dab
F ext/jni/src/org/sqlite/jni/Tester1.java f7b85fe24cf6c3e43bdf7e390617657e8137359f804d76921829c2a8c41b6df1
F ext/jni/src/org/sqlite/jni/Tester1.java 92d4e9dcf9268e6e2588c1cbd808454926ee40287eb9126b0e5eb823918e538d
F ext/jni/src/org/sqlite/jni/Tester2.java 70e005d41060e398ec0f69bd39a8e1c376fd51f81629cf25e877889ec9cb6ec6
F ext/jni/src/org/sqlite/jni/TesterFts5.java d60fe9944a81156b3b5325dd1b0e8e92a1547468f39fd1266d06f7bb6a95fa70
F ext/jni/src/org/sqlite/jni/TraceV2Callback.java f157edd9c72e7d2243c169061487cd7bb51a0d50f3ac976dbcbbacf748ab1fc2
@ -2128,11 +2128,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 2e968114fd0e60eebcc088dec02684e10b06ac1ec42d6bedb5287362f5cbf032
R 64d7e862f198f312cf375ff0e1b4824a
T *branch * api-armor-audit
T *sym-api-armor-audit *
T -sym-trunk * Cancelled\sby\sbranch.
P 8c25c4b18ad07861bf0e47f99f3db04b569b9b859ad0690602f748ddf3576939
R 540c998e288c6bc086b36558f89d784a
U stephan
Z fb0f8b82a9c86be7bcd5823447ca13df
Z 6aa47de4921539f80453f46d0771c8db
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
8c25c4b18ad07861bf0e47f99f3db04b569b9b859ad0690602f748ddf3576939
e5eace747ae656f1e05a346e53a2827242e57107567d2c3470e292cf0b40858d