Skip to content

Commit

Permalink
db_sqlite: Fix some mis-placed NULL checks during row reallocs
Browse files Browse the repository at this point in the history
(cherry picked from commit 1cc9f5d)
  • Loading branch information
liviuchircu committed Sep 25, 2024
1 parent 8b19e1d commit c667f1a
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions modules/db_sqlite/res.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,29 +299,31 @@ int db_sqlite_realloc_rows(db_res_t* res, const unsigned int rows)
struct db_row* res_rows;
db_val_t *prev_values;

res->rows = pkg_realloc(RES_ROWS(res),rows * (sizeof(db_row_t)));
memset( res->rows + RES_ROW_N(res), 0 ,
(rows - RES_ROW_N(res)) * (sizeof(db_row_t)));
if (RES_ROW_N(res) >= rows)
return 0;

res_rows = res->rows;
res_rows = res->rows = pkg_realloc(RES_ROWS(res),rows * sizeof *res_rows);
if (!res_rows) {
LM_ERR("no memory left\n");
return -1;
}

memset(res_rows + RES_ROW_N(res), 0 ,
(rows - RES_ROW_N(res)) * sizeof *res_rows);

prev_values = res_rows[0].values;

res_rows[0].values =
pkg_realloc(res_rows[0].values, rows * sizeof(db_val_t) * RES_COL_N(res));
memset( res_rows[0].values + RES_COL_N(res)*RES_ROW_N(res),
0, (rows - RES_ROW_N(res)) * sizeof(db_val_t) * RES_COL_N(res));

if (! res_rows[0].values) {
LM_ERR("no memory left\n");
res_rows[0].values = prev_values;
return -1;
}

memset( res_rows[0].values + RES_COL_N(res)*RES_ROW_N(res),
0, (rows - RES_ROW_N(res)) * sizeof(db_val_t) * RES_COL_N(res));

/* if the values was relocated, we need to re-point all values to the new block,
* otherwise only fix the new ones */
start = (res_rows[0].values == prev_values?RES_ROW_N(res):0);
Expand All @@ -330,7 +332,7 @@ int db_sqlite_realloc_rows(db_res_t* res, const unsigned int rows)
for( i=start ; i<rows ; i++ ) {
/* the values of the row i */
res_rows[i].values = res_rows[0].values + RES_COL_N(res)*i;
res->rows[i].n = RES_COL_N(res);
res_rows[i].n = RES_COL_N(res);
}

return 0;
Expand Down

0 comments on commit c667f1a

Please sign in to comment.