Skip to content

Commit a6f98ba

Browse files
committed
Prevent crashes due to uninitialized cipher tables
Using the cipher configuration API functions could lead to crashes, if the global cipher tables were not yet initialized.
1 parent 7b3ef7a commit a6f98ba

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

src/cipher_config.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ sqlite3mc_config(sqlite3* db, const char* paramName, int newValue)
5454
int hasMaxPrefix = 0;
5555
CipherParams* param;
5656

57+
#ifndef SQLITE_OMIT_AUTOINIT
58+
if (sqlite3_initialize()) return value;
59+
#endif
60+
5761
if (paramName == NULL || (db == NULL && newValue >= 0))
5862
{
5963
return value;
@@ -134,14 +138,22 @@ sqlite3mc_config(sqlite3* db, const char* paramName, int newValue)
134138
SQLITE_API int
135139
sqlite3mc_cipher_count()
136140
{
141+
#ifndef SQLITE_OMIT_AUTOINIT
142+
if (sqlite3_initialize()) return 0;
143+
#endif
137144
return sqlite3mcGetGlobalCipherCount();
138145
}
139146

140147
SQLITE_API int
141148
sqlite3mc_cipher_index(const char* cipherName)
142149
{
143-
int count = sqlite3mcGetGlobalCipherCount();
144-
int j = 0;
150+
int count;
151+
int j;
152+
#ifndef SQLITE_OMIT_AUTOINIT
153+
if (sqlite3_initialize()) return -1;
154+
#endif
155+
count = sqlite3mcGetGlobalCipherCount();
156+
j = 0;
145157
for (j = 0; j < count && globalCodecDescriptorTable[j].m_name[0] != 0; ++j)
146158
{
147159
if (sqlite3_stricmp(cipherName, globalCodecDescriptorTable[j].m_name) == 0) break;
@@ -153,8 +165,13 @@ SQLITE_API const char*
153165
sqlite3mc_cipher_name(int cipherIndex)
154166
{
155167
static char cipherName[CIPHER_NAME_MAXLEN] = "";
156-
int count = sqlite3mcGetGlobalCipherCount();
157-
int j = 0;
168+
int count;
169+
int j;
170+
#ifndef SQLITE_OMIT_AUTOINIT
171+
if( sqlite3_initialize() ) return cipherName;
172+
#endif
173+
count = sqlite3mcGetGlobalCipherCount();
174+
j = 0;
158175
cipherName[0] = '\0';
159176
if (cipherIndex > 0 && cipherIndex <= count)
160177
{
@@ -179,6 +196,10 @@ sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramNa
179196
CipherParams* cipherParamTable = NULL;
180197
int j = 0;
181198

199+
#ifndef SQLITE_OMIT_AUTOINIT
200+
if (sqlite3_initialize()) return value;
201+
#endif
202+
182203
if (cipherName == NULL || paramName == NULL)
183204
{
184205
sqlite3_log(SQLITE_WARNING,
@@ -246,7 +267,7 @@ sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramNa
246267
{
247268
sqlite3mcConfigureSQLCipherVersion(db, hasDefaultPrefix, newValue);
248269
}
249-
else
270+
else if (newValue != -1)
250271
{
251272
sqlite3_log(SQLITE_WARNING,
252273
"sqlite3mc_config_cipher: SQLCipher legacy version %d out of range [%d..%d]",
@@ -282,7 +303,7 @@ sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramNa
282303
param->m_value = newValue;
283304
value = newValue;
284305
}
285-
else
306+
else if (newValue != -1)
286307
{
287308
sqlite3_log(SQLITE_WARNING,
288309
"sqlite3mc_config_cipher: Value %d for parameter '%s' of cipher '%s' out of range [%d..%d]",
@@ -306,6 +327,9 @@ SQLITE_API unsigned char*
306327
sqlite3mc_codec_data(sqlite3* db, const char* zDbName, const char* paramName)
307328
{
308329
unsigned char* result = NULL;
330+
#ifndef SQLITE_OMIT_AUTOINIT
331+
if (sqlite3_initialize()) return NULL;
332+
#endif
309333
if (db != NULL && paramName != NULL)
310334
{
311335
int iDb = (zDbName != NULL) ? sqlite3FindDbName(db, zDbName) : 0;

0 commit comments

Comments
 (0)