set the error state from the codec when the plaintext_header_size is invalid

This commit is contained in:
Stephen Lombardo 2020-12-01 10:52:34 -05:00
parent dd2f0b5501
commit ba14070de1
3 changed files with 13 additions and 3 deletions

View File

@ -330,8 +330,9 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
if(ctx) {
if( zRight ) {
int size = atoi(zRight);
if((rc = sqlcipher_codec_ctx_set_plaintext_header_size(ctx, size)) != SQLITE_OK)
sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
/* deliberately ignore result code, if size is invalid it will be set to -1
and trip the error later in the codec */
sqlcipher_codec_ctx_set_plaintext_header_size(ctx, size);
} else {
char *size = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_plaintext_header_size(ctx));
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) {
return NULL;
}
/* if the plaintext_header_size is negative that means an invalid size was set via
PRAGMA. We can't set the error state on the pager at that point because the pager
may not be open yet. However, this is a fatal error state, so abort the codec */
if(plaintext_header_sz < 0) {
sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
return NULL;
}
if(pgno == 1) /* adjust starting pointers in data page for header offset on first page*/
offset = plaintext_header_sz ? plaintext_header_sz : FILE_HEADER_SZ;

View File

@ -665,6 +665,7 @@ int sqlcipher_codec_ctx_set_plaintext_header_size(codec_ctx *ctx, int size) {
ctx->plaintext_header_sz = size;
return SQLITE_OK;
}
ctx->plaintext_header_sz = -1;
return SQLITE_ERROR;
}

View File

@ -183,7 +183,7 @@ do_test test-invalid-plaintext-header-sizes {
PRAGMA cipher_plaintext_header_size = 24;
CREATE TABLE t1(a,b);
"]
} {{1 {SQL logic error}} {1 {SQL logic error}} {1 {SQL logic error}}}
} {{1 {out of memory}} {1 {out of memory}} {1 {out of memory}}}
db close
file delete -force test.db