Skip to content

Commit cab2968

Browse files
committed
db_sqlite: Improve error handling
* avoid dangling PS pointers * avoid leaking the PS object on NULL "result" (cherry picked from commit bdd97c5)
1 parent a13d98a commit cab2968

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

modules/db_sqlite/dbase.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ int db_sqlite_query(const db_con_t* _h, const db_key_t* _k, const db_op_t* _op,
193193
if (db_sqlite_bind_values(CON_SQLITE_PS(_h), _v, _n) != SQLITE_OK) {
194194
LM_ERR("failed to bind values\n");
195195
sqlite3_finalize(CON_SQLITE_PS(_h));
196+
CON_SQLITE_PS(_h) = NULL;
196197
return -1;
197198
}
198199
#endif
@@ -209,7 +210,12 @@ int db_sqlite_query(const db_con_t* _h, const db_key_t* _k, const db_op_t* _op,
209210
}
210211
}
211212
if( ret < 0 ){
212-
db_sqlite_free_result_internal(_h,*_r);
213+
if (_r) {
214+
db_sqlite_free_result_internal(_h,*_r);
215+
} else if (CON_SQLITE_PS(_h)) {
216+
sqlite3_finalize(CON_SQLITE_PS(_h));
217+
CON_SQLITE_PS(_h) = NULL;
218+
}
213219
}
214220

215221
return ret;
@@ -408,7 +414,12 @@ int db_sqlite_raw_query(const db_con_t* _h, const str* _s, db_res_t** _r)
408414
}
409415
}
410416
if( ret < 0 ){
411-
db_sqlite_free_result_internal(_h,*_r);
417+
if (_r) {
418+
db_sqlite_free_result_internal(_h,*_r);
419+
} else if (CON_SQLITE_PS(_h)) {
420+
sqlite3_finalize(CON_SQLITE_PS(_h));
421+
CON_SQLITE_PS(_h) = NULL;
422+
}
412423
}
413424

414425
return ret;

0 commit comments

Comments
 (0)