Skip to content

Commit 18f95eb

Browse files
Check for valid context before using cipher_store_pass
Identified by Nicholas Starke
1 parent 32117b0 commit 18f95eb

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/crypto.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,16 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
9797
}
9898
} else
9999
if( sqlite3StrICmp(zLeft, "cipher_store_pass")==0 && zRight ) {
100-
sqlcipher_codec_set_store_pass(ctx, sqlite3GetBoolean(zRight, 1));
100+
if(ctx) {
101+
sqlcipher_codec_set_store_pass(ctx, sqlite3GetBoolean(zRight, 1));
102+
}
101103
} else
102104
if( sqlite3StrICmp(zLeft, "cipher_store_pass")==0 && !zRight ) {
103-
char *store_pass_value = sqlite3_mprintf("%d", sqlcipher_codec_get_store_pass(ctx));
104-
codec_vdbe_return_static_string(pParse, "cipher_store_pass", store_pass_value);
105-
sqlite3_free(store_pass_value);
105+
if(ctx){
106+
char *store_pass_value = sqlite3_mprintf("%d", sqlcipher_codec_get_store_pass(ctx));
107+
codec_vdbe_return_static_string(pParse, "cipher_store_pass", store_pass_value);
108+
sqlite3_free(store_pass_value);
109+
}
106110
}
107111
if( sqlite3StrICmp(zLeft, "cipher_profile")== 0 && zRight ){
108112
char *profile_status = sqlite3_mprintf("%d", sqlcipher_cipher_profile(db, zRight));

test/crypto.test

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,7 @@ db close
19231923
file delete -force test.db
19241924

19251925
# verify invalid cipher does not cause segfault
1926-
if_built_with_openssl verify-invalid-cipher-does_not_segfault {
1926+
if_built_with_openssl verify-invalid-cipher-does-not-segfault {
19271927
sqlite_orig db test.db
19281928
execsql {
19291929
PRAGMA key = 'test';
@@ -1934,6 +1934,18 @@ if_built_with_openssl verify-invalid-cipher-does_not_segfault {
19341934
db close
19351935
file delete -force test.db
19361936

1937+
# verify setting cipher_store_pass before key
1938+
# does not cause segfault
1939+
do_test verify-cipher-store-pass-before-key-does-not-segfault {
1940+
sqlite_orig db test.db
1941+
execsql {
1942+
PRAGMA cipher_store_pass = 1;
1943+
PRAGMA key = 'test';
1944+
}
1945+
} {}
1946+
db close
1947+
file delete -force test.db
1948+
19371949
# verify the pragma cipher
19381950
# reports the default value
19391951
if_built_with_openssl verify-pragma-cipher-default {

0 commit comments

Comments
 (0)