Skip to content

Commit ba14070

Browse files
committed
set the error state from the codec when the plaintext_header_size is invalid
1 parent dd2f0b5 commit ba14070

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

src/crypto.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,9 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
330330
if(ctx) {
331331
if( zRight ) {
332332
int size = atoi(zRight);
333-
if((rc = sqlcipher_codec_ctx_set_plaintext_header_size(ctx, size)) != SQLITE_OK)
334-
sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
333+
/* deliberately ignore result code, if size is invalid it will be set to -1
334+
and trip the error later in the codec */
335+
sqlcipher_codec_ctx_set_plaintext_header_size(ctx, size);
335336
} else {
336337
char *size = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_plaintext_header_size(ctx));
337338
codec_vdbe_return_string(pParse, "cipher_plaintext_header_size", size, P4_DYNAMIC);
@@ -697,6 +698,14 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
697698
return NULL;
698699
}
699700

701+
/* if the plaintext_header_size is negative that means an invalid size was set via
702+
PRAGMA. We can't set the error state on the pager at that point because the pager
703+
may not be open yet. However, this is a fatal error state, so abort the codec */
704+
if(plaintext_header_sz < 0) {
705+
sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
706+
return NULL;
707+
}
708+
700709
if(pgno == 1) /* adjust starting pointers in data page for header offset on first page*/
701710
offset = plaintext_header_sz ? plaintext_header_sz : FILE_HEADER_SZ;
702711

src/crypto_impl.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ int sqlcipher_codec_ctx_set_plaintext_header_size(codec_ctx *ctx, int size) {
665665
ctx->plaintext_header_sz = size;
666666
return SQLITE_OK;
667667
}
668+
ctx->plaintext_header_sz = -1;
668669
return SQLITE_ERROR;
669670
}
670671

test/sqlcipher-plaintext-header.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ do_test test-invalid-plaintext-header-sizes {
183183
PRAGMA cipher_plaintext_header_size = 24;
184184
CREATE TABLE t1(a,b);
185185
"]
186-
} {{1 {SQL logic error}} {1 {SQL logic error}} {1 {SQL logic error}}}
186+
} {{1 {out of memory}} {1 {out of memory}} {1 {out of memory}}}
187187
db close
188188
file delete -force test.db
189189

0 commit comments

Comments
 (0)