From 913022cc3e1ca82edff1798b262588d886af514a Mon Sep 17 00:00:00 2001 From: Stephen Lombardo Date: Wed, 18 Jul 2012 14:40:48 -0400 Subject: [PATCH] detect failure to initialize context (i.e. out of memory) --- src/crypto.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/crypto.c b/src/crypto.c index ad722eb1..abca0769 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -223,6 +223,10 @@ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) { /* point the internal codec argument against the contet to be prepared */ rc = sqlcipher_codec_ctx_init(&ctx, pDb, pDb->pBt->pBt->pPager, fd, zKey, nKey); + if(rc != SQLITE_OK) return rc; /* initialization failed, do not attach potentially corrupted context */ + + sqlite3_mutex_enter(db->mutex); + sqlite3pager_sqlite3PagerSetCodec(sqlite3BtreePager(pDb->pBt), sqlite3Codec, NULL, sqlite3FreeCodecArg, (void *) ctx); codec_set_btree_to_codec_pagesize(db, pDb, ctx); @@ -235,7 +239,6 @@ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) { /* if fd is null, then this is an in-memory database and we dont' want to overwrite the AutoVacuum settings if not null, then set to the default */ - sqlite3_mutex_enter(db->mutex); if(fd != NULL) { sqlite3BtreeSetAutoVacuum(pDb->pBt, SQLITE_DEFAULT_AUTOVACUUM); } @@ -252,8 +255,7 @@ int sqlite3_key(sqlite3 *db, const void *pKey, int nKey) { CODEC_TRACE(("sqlite3_key: entered db=%p pKey=%s nKey=%d\n", db, (char *)pKey, nKey)); /* attach key if db and pKey are not null and nKey is > 0 */ if(db && pKey && nKey) { - sqlite3CodecAttach(db, 0, pKey, nKey); // operate only on the main db - return SQLITE_OK; + return sqlite3CodecAttach(db, 0, pKey, nKey); // operate only on the main db } return SQLITE_ERROR; }