Skip to content

Commit

Permalink
db_sqlite: fix memory leak for row values
Browse files Browse the repository at this point in the history
(cherry picked from commit 06a35a6)
  • Loading branch information
razvancrainea committed Nov 2, 2021
1 parent c493e01 commit 5ac273d
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions modules/db_sqlite/dbase.c
Expand Up @@ -55,6 +55,7 @@ static int db_sqlite_store_result(const db_con_t* _h, db_res_t** _r, const db_va
static int db_sqlite_bind_values(sqlite3_stmt* stmt, const db_val_t* _v, const int _n);
#endif
static int db_sqlite_free_result_internal(const db_con_t* _h, db_res_t* _r);
static void db_sqlite_free_result_rows(db_res_t* _r);

static int db_sqlite_submit_dummy_query(const db_con_t* _h, const str* _s)
{
Expand Down Expand Up @@ -265,10 +266,7 @@ int db_sqlite_fetch_result(const db_con_t* _h, db_res_t** _r, const int nrows)
}
} else {
/* free old rows */
if(RES_ROWS(*_r)!=0)
db_free_rows(*_r);
RES_ROWS(*_r) = 0;
RES_ROW_N(*_r) = 0;
db_sqlite_free_result_rows(*_r);
}

/* determine the number of rows remaining to be processed */
Expand Down Expand Up @@ -824,6 +822,32 @@ int db_sqlite_free_result(db_con_t* _h, db_res_t* _r)
return 0;
}

/**
* Release a result set from memory.
* \param _r result set whose rows and values should be freed
* \return void
*/
static void db_sqlite_free_result_rows(db_res_t* _r)
{
db_val_t* values;

if (!_r) {
LM_DBG("nothing to free!\n");
return;
}

if(RES_ROWS(_r)!=0)
{
values = _r->rows[0].values;
/* db_sqlite_allocate_rows allocates memory for rows and values separately.
* Hence freeing rows using generic function and then values separately*/
db_free_rows(_r);
pkg_free(values);
}
RES_ROWS(_r) = 0;
RES_ROW_N(_r) = 0;
}

/**
* Retrieve a result set
* \param _h handle to the database
Expand Down

0 comments on commit 5ac273d

Please sign in to comment.