Skip to content

Commit 148c2bf

Browse files
committed
skip uneccesary sqlcipher_free calls
1 parent 4d9f498 commit 148c2bf

1 file changed

Lines changed: 16 additions & 14 deletions

File tree

src/crypto_impl.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,10 @@ static int sqlcipher_cipher_ctx_init(codec_ctx *ctx, cipher_ctx **iCtx) {
468468
static void sqlcipher_cipher_ctx_free(codec_ctx* ctx, cipher_ctx **iCtx) {
469469
cipher_ctx *c_ctx = *iCtx;
470470
sqlcipher_log(SQLCIPHER_LOG_DEBUG, "cipher_ctx_free: iCtx=%p", iCtx);
471-
sqlcipher_free(c_ctx->key, ctx->key_sz);
472-
sqlcipher_free(c_ctx->hmac_key, ctx->key_sz);
473-
sqlcipher_free(c_ctx->pass, c_ctx->pass_sz);
474-
sqlcipher_free(c_ctx->keyspec, ctx->keyspec_sz);
471+
if(c_ctx->key) sqlcipher_free(c_ctx->key, ctx->key_sz);
472+
if(c_ctx->hmac_key) sqlcipher_free(c_ctx->hmac_key, ctx->key_sz);
473+
if(c_ctx->pass) sqlcipher_free(c_ctx->pass, c_ctx->pass_sz);
474+
if(c_ctx->keyspec) sqlcipher_free(c_ctx->keyspec, ctx->keyspec_sz);
475475
sqlcipher_free(c_ctx, sizeof(cipher_ctx));
476476
}
477477

@@ -542,8 +542,8 @@ static int sqlcipher_cipher_ctx_copy(codec_ctx *ctx, cipher_ctx *target, cipher_
542542
void *hmac_key = target->hmac_key;
543543

544544
sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_cipher_ctx_copy: target=%p, source=%p", target, source);
545-
sqlcipher_free(target->pass, target->pass_sz);
546-
sqlcipher_free(target->keyspec, ctx->keyspec_sz);
545+
if(target->pass) sqlcipher_free(target->pass, target->pass_sz);
546+
if(target->keyspec) sqlcipher_free(target->keyspec, ctx->keyspec_sz);
547547
memcpy(target, source, sizeof(cipher_ctx));
548548

549549
target->key = key; /* restore pointer to previously allocated key data */
@@ -573,7 +573,7 @@ static int sqlcipher_cipher_ctx_copy(codec_ctx *ctx, cipher_ctx *target, cipher_
573573
*/
574574
static int sqlcipher_cipher_ctx_set_keyspec(codec_ctx *ctx, cipher_ctx *c_ctx, const unsigned char *key) {
575575
/* free, zero existing pointers and size */
576-
sqlcipher_free(c_ctx->keyspec, ctx->keyspec_sz);
576+
if(c_ctx->keyspec) sqlcipher_free(c_ctx->keyspec, ctx->keyspec_sz);
577577
c_ctx->keyspec = NULL;
578578

579579
c_ctx->keyspec = sqlcipher_malloc(ctx->keyspec_sz);
@@ -613,7 +613,7 @@ static void sqlcipher_set_derive_key(codec_ctx *ctx, int derive) {
613613
*/
614614
static int sqlcipher_cipher_ctx_set_pass(cipher_ctx *ctx, const void *zKey, int nKey) {
615615
/* free, zero existing pointers and size */
616-
sqlcipher_free(ctx->pass, ctx->pass_sz);
616+
if(ctx->pass) sqlcipher_free(ctx->pass, ctx->pass_sz);
617617
ctx->pass = NULL;
618618
ctx->pass_sz = 0;
619619

@@ -846,7 +846,7 @@ int sqlcipher_codec_ctx_set_pagesize(codec_ctx *ctx, int size) {
846846
return SQLITE_ERROR;
847847
}
848848
/* attempt to free the existing page buffer */
849-
sqlcipher_free(ctx->buffer,ctx->page_sz);
849+
if(ctx->buffer) sqlcipher_free(ctx->buffer,ctx->page_sz);
850850
ctx->page_sz = size;
851851

852852
/* pre-allocate a page buffer of PageSize bytes. This will
@@ -1025,12 +1025,14 @@ int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, const voi
10251025
void sqlcipher_codec_ctx_free(codec_ctx **iCtx) {
10261026
codec_ctx *ctx = *iCtx;
10271027
sqlcipher_log(SQLCIPHER_LOG_DEBUG, "codec_ctx_free: iCtx=%p", iCtx);
1028-
sqlcipher_free(ctx->kdf_salt, ctx->kdf_salt_sz);
1029-
sqlcipher_free(ctx->hmac_kdf_salt, ctx->kdf_salt_sz);
1030-
sqlcipher_free(ctx->buffer, ctx->page_sz);
1028+
if(ctx->kdf_salt) sqlcipher_free(ctx->kdf_salt, ctx->kdf_salt_sz);
1029+
if(ctx->hmac_kdf_salt) sqlcipher_free(ctx->hmac_kdf_salt, ctx->kdf_salt_sz);
1030+
if(ctx->buffer) sqlcipher_free(ctx->buffer, ctx->page_sz);
10311031

1032-
ctx->provider->ctx_free(&ctx->provider_ctx);
1033-
sqlcipher_free(ctx->provider, sizeof(sqlcipher_provider));
1032+
if(ctx->provider) {
1033+
ctx->provider->ctx_free(&ctx->provider_ctx);
1034+
sqlcipher_free(ctx->provider, sizeof(sqlcipher_provider));
1035+
}
10341036

10351037
sqlcipher_cipher_ctx_free(ctx, &ctx->read_ctx);
10361038
sqlcipher_cipher_ctx_free(ctx, &ctx->write_ctx);

0 commit comments

Comments
 (0)