Skip to content

Commit 537dcf7

Browse files
committed
improve memory handling for values returned from pragmas
1 parent 787cbae commit 537dcf7

1 file changed

Lines changed: 45 additions & 65 deletions

File tree

src/crypto.c

Lines changed: 45 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
#endif
4141

4242
/* Generate code to return a string value */
43-
static void codec_vdbe_return_static_string(Parse *pParse, const char *zLabel, const char *value){
43+
static void codec_vdbe_return_string(Parse *pParse, const char *zLabel, const char *value, int value_type){
4444
Vdbe *v = sqlite3GetVdbe(pParse);
4545
sqlite3VdbeSetNumCols(v, 1);
4646
sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
47-
sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, value, 0);
47+
sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, value, value_type);
4848
sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
4949
}
5050

@@ -100,24 +100,21 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
100100
#ifdef SQLCIPHER_LICENSE
101101
if( sqlite3StrICmp(zLeft, "cipher_license")==0 && zRight ){
102102
char *license_result = sqlite3_mprintf("%d", sqlcipher_license_key(zRight));
103-
codec_vdbe_return_static_string(pParse, "cipher_license", license_result);
104-
sqlite3_free(license_result);
103+
codec_vdbe_return_string(pParse, "cipher_license", license_result, P4_DYNAMIC);
105104
} else
106105
if( sqlite3StrICmp(zLeft, "cipher_license")==0 && !zRight ){
107106
if(ctx) {
108107
char *license_result = sqlite3_mprintf("%d", ctx
109108
? sqlcipher_license_key_status(ctx)
110109
: SQLITE_ERROR);
111-
codec_vdbe_return_static_string(pParse, "cipher_license", license_result);
112-
sqlite3_free(license_result);
110+
codec_vdbe_return_string(pParse, "cipher_license", license_result, P4_DYNAMIC);
113111
}
114112
} else
115113
#endif
116114
if( sqlite3StrICmp(zLeft, "cipher_fips_status")== 0 && !zRight ){
117115
if(ctx) {
118116
char *fips_mode_status = sqlite3_mprintf("%d", sqlcipher_codec_fips_status(ctx));
119-
codec_vdbe_return_static_string(pParse, "cipher_fips_status", fips_mode_status);
120-
sqlite3_free(fips_mode_status);
117+
codec_vdbe_return_string(pParse, "cipher_fips_status", fips_mode_status, P4_DYNAMIC);
121118
}
122119
} else
123120
if( sqlite3StrICmp(zLeft, "cipher_store_pass")==0 && zRight ) {
@@ -128,37 +125,33 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
128125
if( sqlite3StrICmp(zLeft, "cipher_store_pass")==0 && !zRight ) {
129126
if(ctx){
130127
char *store_pass_value = sqlite3_mprintf("%d", sqlcipher_codec_get_store_pass(ctx));
131-
codec_vdbe_return_static_string(pParse, "cipher_store_pass", store_pass_value);
132-
sqlite3_free(store_pass_value);
128+
codec_vdbe_return_string(pParse, "cipher_store_pass", store_pass_value, P4_DYNAMIC);
133129
}
134130
}
135131
if( sqlite3StrICmp(zLeft, "cipher_profile")== 0 && zRight ){
136132
char *profile_status = sqlite3_mprintf("%d", sqlcipher_cipher_profile(db, zRight));
137-
codec_vdbe_return_static_string(pParse, "cipher_profile", profile_status);
138-
sqlite3_free(profile_status);
133+
codec_vdbe_return_string(pParse, "cipher_profile", profile_status, P4_DYNAMIC);
139134
} else
140135
if( sqlite3StrICmp(zLeft, "cipher_add_random")==0 && zRight ){
141136
if(ctx) {
142137
char *add_random_status = sqlite3_mprintf("%d", sqlcipher_codec_add_random(ctx, zRight, sqlite3Strlen30(zRight)));
143-
codec_vdbe_return_static_string(pParse, "cipher_add_random", add_random_status);
144-
sqlite3_free(add_random_status);
138+
codec_vdbe_return_string(pParse, "cipher_add_random", add_random_status, P4_DYNAMIC);
145139
}
146140
} else
147141
if( sqlite3StrICmp(zLeft, "cipher_migrate")==0 && !zRight ){
148142
if(ctx){
149143
char *migrate_status = sqlite3_mprintf("%d", sqlcipher_codec_ctx_migrate(ctx));
150-
codec_vdbe_return_static_string(pParse, "cipher_migrate", migrate_status);
151-
sqlite3_free(migrate_status);
144+
codec_vdbe_return_string(pParse, "cipher_migrate", migrate_status, P4_DYNAMIC);
152145
}
153146
} else
154147
if( sqlite3StrICmp(zLeft, "cipher_provider")==0 && !zRight ){
155-
if(ctx) { codec_vdbe_return_static_string(pParse, "cipher_provider",
156-
sqlcipher_codec_get_cipher_provider(ctx));
148+
if(ctx) { codec_vdbe_return_string(pParse, "cipher_provider",
149+
sqlcipher_codec_get_cipher_provider(ctx), P4_TRANSIENT);
157150
}
158151
} else
159152
if( sqlite3StrICmp(zLeft, "cipher_provider_version")==0 && !zRight){
160-
if(ctx) { codec_vdbe_return_static_string(pParse, "cipher_provider_version",
161-
sqlcipher_codec_get_provider_version(ctx));
153+
if(ctx) { codec_vdbe_return_string(pParse, "cipher_provider_version",
154+
sqlcipher_codec_get_provider_version(ctx), P4_TRANSIENT);
162155
}
163156
} else
164157
if( sqlite3StrICmp(zLeft, "cipher_version")==0 && !zRight ){
@@ -167,33 +160,30 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
167160
#else
168161
char *version = sqlite3_mprintf("%s %s", CIPHER_XSTR(CIPHER_VERSION_NUMBER), CIPHER_XSTR(CIPHER_VERSION_BUILD));
169162
#endif
170-
codec_vdbe_return_static_string(pParse, "cipher_version", version);
171-
sqlite3_free(version);
163+
codec_vdbe_return_string(pParse, "cipher_version", version, P4_DYNAMIC);
172164
}else
173165
if( sqlite3StrICmp(zLeft, "cipher")==0 ){
174166
if(ctx) {
175167
if( zRight ) {
176168
const char* message = "PRAGMA cipher is no longer supported.";
177-
codec_vdbe_return_static_string(pParse, "cipher", message);
169+
codec_vdbe_return_string(pParse, "cipher", message, P4_TRANSIENT);
178170
sqlite3_log(SQLITE_WARNING, message);
179171
}else {
180-
codec_vdbe_return_static_string(pParse, "cipher",
181-
sqlcipher_codec_ctx_get_cipher(ctx));
172+
codec_vdbe_return_string(pParse, "cipher", sqlcipher_codec_ctx_get_cipher(ctx), P4_TRANSIENT);
182173
}
183174
}
184175
}else
185176
if( sqlite3StrICmp(zLeft, "rekey_cipher")==0 && zRight ){
186177
const char* message = "PRAGMA rekey_cipher is no longer supported.";
187-
codec_vdbe_return_static_string(pParse, "rekey_cipher", message);
178+
codec_vdbe_return_string(pParse, "rekey_cipher", message, P4_TRANSIENT);
188179
sqlite3_log(SQLITE_WARNING, message);
189180
}else
190181
if( sqlite3StrICmp(zLeft,"cipher_default_kdf_iter")==0 ){
191182
if( zRight ) {
192183
sqlcipher_set_default_kdf_iter(atoi(zRight)); // change default KDF iterations
193184
} else {
194185
char *kdf_iter = sqlite3_mprintf("%d", sqlcipher_get_default_kdf_iter());
195-
codec_vdbe_return_static_string(pParse, "cipher_default_kdf_iter", kdf_iter);
196-
sqlite3_free(kdf_iter);
186+
codec_vdbe_return_string(pParse, "cipher_default_kdf_iter", kdf_iter, P4_DYNAMIC);
197187
}
198188
}else
199189
if( sqlite3StrICmp(zLeft, "kdf_iter")==0 ){
@@ -202,8 +192,7 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
202192
sqlcipher_codec_ctx_set_kdf_iter(ctx, atoi(zRight)); // change of RW PBKDF2 iteration
203193
} else {
204194
char *kdf_iter = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_kdf_iter(ctx));
205-
codec_vdbe_return_static_string(pParse, "kdf_iter", kdf_iter);
206-
sqlite3_free(kdf_iter);
195+
codec_vdbe_return_string(pParse, "kdf_iter", kdf_iter, P4_DYNAMIC);
207196
}
208197
}
209198
}else
@@ -213,14 +202,13 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
213202
sqlcipher_codec_ctx_set_fast_kdf_iter(ctx, atoi(zRight)); // change of RW PBKDF2 iteration
214203
} else {
215204
char *fast_kdf_iter = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_fast_kdf_iter(ctx));
216-
codec_vdbe_return_static_string(pParse, "fast_kdf_iter", fast_kdf_iter);
217-
sqlite3_free(fast_kdf_iter);
205+
codec_vdbe_return_string(pParse, "fast_kdf_iter", fast_kdf_iter, P4_DYNAMIC);
218206
}
219207
}
220208
}else
221209
if( sqlite3StrICmp(zLeft, "rekey_kdf_iter")==0 && zRight ){
222210
const char* message = "PRAGMA rekey_kdf_iter is no longer supported.";
223-
codec_vdbe_return_static_string(pParse, "rekey_kdf_iter", message);
211+
codec_vdbe_return_string(pParse, "rekey_kdf_iter", message, P4_TRANSIENT);
224212
sqlite3_log(SQLITE_WARNING, message);
225213
}else
226214
if( sqlite3StrICmp(zLeft,"cipher_page_size")==0 ){
@@ -233,8 +221,7 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
233221
if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc);
234222
} else {
235223
char * page_size = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_pagesize(ctx));
236-
codec_vdbe_return_static_string(pParse, "cipher_page_size", page_size);
237-
sqlite3_free(page_size);
224+
codec_vdbe_return_string(pParse, "cipher_page_size", page_size, P4_DYNAMIC);
238225
}
239226
}
240227
}else
@@ -243,17 +230,15 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
243230
sqlcipher_set_default_pagesize(atoi(zRight));
244231
} else {
245232
char *default_page_size = sqlite3_mprintf("%d", sqlcipher_get_default_pagesize());
246-
codec_vdbe_return_static_string(pParse, "cipher_default_page_size", default_page_size);
247-
sqlite3_free(default_page_size);
233+
codec_vdbe_return_string(pParse, "cipher_default_page_size", default_page_size, P4_DYNAMIC);
248234
}
249235
}else
250236
if( sqlite3StrICmp(zLeft,"cipher_default_use_hmac")==0 ){
251237
if( zRight ) {
252238
sqlcipher_set_default_use_hmac(sqlite3GetBoolean(zRight,1));
253239
} else {
254240
char *default_use_hmac = sqlite3_mprintf("%d", sqlcipher_get_default_use_hmac());
255-
codec_vdbe_return_static_string(pParse, "cipher_default_use_hmac", default_use_hmac);
256-
sqlite3_free(default_use_hmac);
241+
codec_vdbe_return_string(pParse, "cipher_default_use_hmac", default_use_hmac, P4_DYNAMIC);
257242
}
258243
}else
259244
if( sqlite3StrICmp(zLeft,"cipher_use_hmac")==0 ){
@@ -266,8 +251,7 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
266251
if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc);
267252
} else {
268253
char *hmac_flag = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_use_hmac(ctx));
269-
codec_vdbe_return_static_string(pParse, "cipher_use_hmac", hmac_flag);
270-
sqlite3_free(hmac_flag);
254+
codec_vdbe_return_string(pParse, "cipher_use_hmac", hmac_flag, P4_DYNAMIC);
271255
}
272256
}
273257
}else
@@ -287,11 +271,11 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
287271
}
288272
} else {
289273
if(sqlcipher_codec_ctx_get_flag(ctx, CIPHER_FLAG_LE_PGNO)) {
290-
codec_vdbe_return_static_string(pParse, "cipher_hmac_pgno", "le");
274+
codec_vdbe_return_string(pParse, "cipher_hmac_pgno", "le", P4_TRANSIENT);
291275
} else if(sqlcipher_codec_ctx_get_flag(ctx, CIPHER_FLAG_BE_PGNO)) {
292-
codec_vdbe_return_static_string(pParse, "cipher_hmac_pgno", "be");
276+
codec_vdbe_return_string(pParse, "cipher_hmac_pgno", "be", P4_TRANSIENT);
293277
} else {
294-
codec_vdbe_return_static_string(pParse, "cipher_hmac_pgno", "native");
278+
codec_vdbe_return_string(pParse, "cipher_hmac_pgno", "native", P4_TRANSIENT);
295279
}
296280
}
297281
}
@@ -307,8 +291,7 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
307291
}
308292
} else {
309293
char *hmac_salt_mask = sqlite3_mprintf("%02x", sqlcipher_get_hmac_salt_mask());
310-
codec_vdbe_return_static_string(pParse, "cipher_hmac_salt_mask", hmac_salt_mask);
311-
sqlite3_free(hmac_salt_mask);
294+
codec_vdbe_return_string(pParse, "cipher_hmac_salt_mask", hmac_salt_mask, P4_DYNAMIC);
312295
}
313296
}
314297
}else
@@ -320,8 +303,7 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
320303
sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
321304
} else {
322305
char *size = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_plaintext_header_size(ctx));
323-
codec_vdbe_return_static_string(pParse, "cipher_plaintext_header_size", size);
324-
sqlite3_free(size);
306+
codec_vdbe_return_string(pParse, "cipher_plaintext_header_size", size, P4_DYNAMIC);
325307
}
326308
}
327309
}else
@@ -330,7 +312,7 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
330312
sqlcipher_set_default_plaintext_header_size(atoi(zRight));
331313
} else {
332314
char *size = sqlite3_mprintf("%d", sqlcipher_get_default_plaintext_header_size());
333-
codec_vdbe_return_static_string(pParse, "cipher_default_plaintext_header_size", size);
315+
codec_vdbe_return_string(pParse, "cipher_default_plaintext_header_size", size, P4_DYNAMIC);
334316
sqlite3_free(size);
335317
}
336318
}else
@@ -347,8 +329,7 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
347329
} else {
348330
char *salt = (char*) sqlite3_malloc((FILE_HEADER_SZ*2)+1);
349331
cipher_bin2hex(sqlcipher_codec_ctx_get_kdf_salt(ctx), FILE_HEADER_SZ, salt);
350-
codec_vdbe_return_static_string(pParse, "cipher_salt", salt);
351-
sqlite3_free(salt);
332+
codec_vdbe_return_string(pParse, "cipher_salt", salt, P4_DYNAMIC);
352333
}
353334
}
354335
}else
@@ -369,11 +350,11 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
369350
} else {
370351
int algorithm = sqlcipher_codec_ctx_get_hmac_algorithm(ctx);
371352
if(algorithm == SQLCIPHER_HMAC_SHA1) {
372-
codec_vdbe_return_static_string(pParse, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA1_LABEL);
353+
codec_vdbe_return_string(pParse, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA1_LABEL, P4_TRANSIENT);
373354
} else if(algorithm == SQLCIPHER_HMAC_SHA256) {
374-
codec_vdbe_return_static_string(pParse, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA256_LABEL);
355+
codec_vdbe_return_string(pParse, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA256_LABEL, P4_TRANSIENT);
375356
} else if(algorithm == SQLCIPHER_HMAC_SHA512) {
376-
codec_vdbe_return_static_string(pParse, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA512_LABEL);
357+
codec_vdbe_return_string(pParse, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA512_LABEL, P4_TRANSIENT);
377358
}
378359
}
379360
}
@@ -392,11 +373,11 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
392373
} else {
393374
int algorithm = sqlcipher_get_default_hmac_algorithm();
394375
if(algorithm == SQLCIPHER_HMAC_SHA1) {
395-
codec_vdbe_return_static_string(pParse, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA1_LABEL);
376+
codec_vdbe_return_string(pParse, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA1_LABEL, P4_TRANSIENT);
396377
} else if(algorithm == SQLCIPHER_HMAC_SHA256) {
397-
codec_vdbe_return_static_string(pParse, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA256_LABEL);
378+
codec_vdbe_return_string(pParse, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA256_LABEL, P4_TRANSIENT);
398379
} else if(algorithm == SQLCIPHER_HMAC_SHA512) {
399-
codec_vdbe_return_static_string(pParse, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA512_LABEL);
380+
codec_vdbe_return_string(pParse, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA512_LABEL, P4_TRANSIENT);
400381
}
401382
}
402383
}else
@@ -415,11 +396,11 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
415396
} else {
416397
int algorithm = sqlcipher_codec_ctx_get_kdf_algorithm(ctx);
417398
if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA1) {
418-
codec_vdbe_return_static_string(pParse, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL);
399+
codec_vdbe_return_string(pParse, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL, P4_TRANSIENT);
419400
} else if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA256) {
420-
codec_vdbe_return_static_string(pParse, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL);
401+
codec_vdbe_return_string(pParse, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL, P4_TRANSIENT);
421402
} else if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA512) {
422-
codec_vdbe_return_static_string(pParse, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL);
403+
codec_vdbe_return_string(pParse, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL, P4_TRANSIENT);
423404
}
424405
}
425406
}
@@ -438,11 +419,11 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
438419
} else {
439420
int algorithm = sqlcipher_get_default_kdf_algorithm();
440421
if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA1) {
441-
codec_vdbe_return_static_string(pParse, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL);
422+
codec_vdbe_return_string(pParse, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL, P4_TRANSIENT);
442423
} else if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA256) {
443-
codec_vdbe_return_static_string(pParse, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL);
424+
codec_vdbe_return_string(pParse, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL, P4_TRANSIENT);
444425
} else if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA512) {
445-
codec_vdbe_return_static_string(pParse, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL);
426+
codec_vdbe_return_string(pParse, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL, P4_TRANSIENT);
446427
}
447428
}
448429
}else
@@ -451,8 +432,7 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
451432
sqlcipher_set_mem_security(sqlite3GetBoolean(zRight,1));
452433
} else {
453434
char *on = sqlite3_mprintf("%d", sqlcipher_get_mem_security());
454-
codec_vdbe_return_static_string(pParse, "cipher_memory_security", on);
455-
sqlite3_free(on);
435+
codec_vdbe_return_string(pParse, "cipher_memory_security", on, P4_DYNAMIC);
456436
}
457437
}else {
458438
return 0;

0 commit comments

Comments
 (0)