Skip to content

Commit cb71f53

Browse files
committed
fix sqlcipher_export handling of NULL parameters
1 parent ba14070 commit cb71f53

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

src/crypto.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,13 +1075,29 @@ void sqlcipher_exportFunc(sqlite3_context *context, int argc, sqlite3_value **ar
10751075
goto end_of_export;
10761076
}
10771077

1078-
targetDb = (const char*) sqlite3_value_text(argv[0]);
1079-
sourceDb = (argc == 2) ? (char *) sqlite3_value_text(argv[1]) : "main";
1078+
if(sqlite3_value_type(argv[0]) == SQLITE_NULL) {
1079+
rc = SQLITE_ERROR;
1080+
pzErrMsg = sqlite3_mprintf("target database can't be NULL");
1081+
goto end_of_export;
1082+
}
1083+
1084+
targetDb = (const char*) sqlite3_value_text(argv[0]);
1085+
sourceDb = "main";
1086+
1087+
if(argc == 2) {
1088+
if(sqlite3_value_type(argv[1]) == SQLITE_NULL) {
1089+
rc = SQLITE_ERROR;
1090+
pzErrMsg = sqlite3_mprintf("target database can't be NULL");
1091+
goto end_of_export;
1092+
}
1093+
sourceDb = (char *) sqlite3_value_text(argv[1]);
1094+
}
1095+
10801096

10811097
/* if the name of the target is not main, but the index returned is zero
10821098
there is a mismatch and we should not proceed */
10831099
targetDb_idx = sqlcipher_find_db_index(db, targetDb);
1084-
if(targetDb_idx == 0 && sqlite3StrICmp("main", targetDb) != 0) {
1100+
if(targetDb_idx == 0 && targetDb != NULL && sqlite3StrICmp("main", targetDb) != 0) {
10851101
rc = SQLITE_ERROR;
10861102
pzErrMsg = sqlite3_mprintf("unknown database %s", targetDb);
10871103
goto end_of_export;

test/sqlcipher-compatibility.test

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,32 @@ do_test export-error {
444444
db close
445445
file delete -force test.db
446446

447+
# verify sqlcipher_export with NULL parameters
448+
do_test export-nulls {
449+
sqlite_orig db test.db
450+
451+
catchsql {
452+
SELECT sqlcipher_export(NULL);
453+
}
454+
455+
} {1 {target database can't be NULL}}
456+
db close
457+
file delete -force test.db
458+
459+
do_test export-nulls {
460+
sqlite_orig db test.db
461+
462+
catchsql {
463+
SELECT sqlcipher_export('main', NULL);
464+
}
465+
466+
} {1 {target database can't be NULL}}
467+
db close
468+
file delete -force test.db
469+
470+
471+
# use the sqlcipher_export function
472+
447473
# use the sqlcipher_export function
448474
# to copy a complicated database.
449475
# tests autoincrement fields,

0 commit comments

Comments
 (0)