Skip to content

Commit bdd97c5

Browse files
committed
db_sqlite: Improve error handling
* avoid dangling PS pointers * avoid leaking the PS object on NULL "result"
1 parent 5abc95a commit bdd97c5

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

modules/db_sqlite/dbase.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ int db_sqlite_query(const db_con_t* _h, const db_key_t* _k, const db_op_t* _op,
183183
if (db_sqlite_bind_values(CON_SQLITE_PS(_h), _v, _n) != SQLITE_OK) {
184184
LM_ERR("failed to bind values\n");
185185
sqlite3_finalize(CON_SQLITE_PS(_h));
186+
CON_SQLITE_PS(_h) = NULL;
186187
return -1;
187188
}
188189
#endif
@@ -198,8 +199,13 @@ int db_sqlite_query(const db_con_t* _h, const db_key_t* _k, const db_op_t* _op,
198199
ret = 0;
199200
}
200201
}
201-
if( ret < 0 && _r ){
202-
db_sqlite_free_result_internal(_h,*_r);
202+
if( ret < 0 ) {
203+
if (_r) {
204+
db_sqlite_free_result_internal(_h,*_r);
205+
} else if (CON_SQLITE_PS(_h)) {
206+
sqlite3_finalize(CON_SQLITE_PS(_h));
207+
CON_SQLITE_PS(_h) = NULL;
208+
}
203209
}
204210

205211
return ret;
@@ -394,8 +400,13 @@ int db_sqlite_raw_query(const db_con_t* _h, const str* _s, db_res_t** _r)
394400
ret = 0;
395401
}
396402
}
397-
if( ret < 0 && _r ){
398-
db_sqlite_free_result_internal(_h,*_r);
403+
if( ret < 0 ){
404+
if (_r) {
405+
db_sqlite_free_result_internal(_h,*_r);
406+
} else if (CON_SQLITE_PS(_h)) {
407+
sqlite3_finalize(CON_SQLITE_PS(_h));
408+
CON_SQLITE_PS(_h) = NULL;
409+
}
399410
}
400411

401412
return ret;

0 commit comments

Comments
 (0)