Skip to content

Commit

Permalink
dialog: Mitigate 'dlg_list' PKG fragmentation; Fix dlg_list_ctx memleak
Browse files Browse the repository at this point in the history
By using pkg_str_extend() instead of pkg_realloc(), we avoid a constant
creation of PKG fragments (the buffer will eventually stop growing),
which seems to gradually fragment the private memory pool, as the MI
'dlg_list' is continuously polled by various monitoring software.

As a bonus, this patch also fixes a 'dlg_list_ctx' PKG memory leak
introduced in 543a40c and only present on OpenSIPS 3.4+.

Related to #3235

(cherry picked from commit 34dc02b)
  • Loading branch information
liviuchircu committed Nov 15, 2023
1 parent 1a00e1e commit 95a27fe
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions modules/dialog/dlg_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -1339,7 +1339,7 @@ void next_state_dlg(struct dlg_cell *dlg, int event, int dir, int *old_state,


/**************************** MI functions ******************************/
static char *dlg_val_buf;
static str dlg_val_buf;
static inline int internal_mi_print_dlg(mi_item_t *dialog_obj,
struct dlg_cell *dlg, int with_context)
{
Expand Down Expand Up @@ -1496,11 +1496,12 @@ static inline int internal_mi_print_dlg(mi_item_t *dialog_obj,
for( dv=dlg->vals ; dv ; dv=dv->next) {
if (dv->type == DLG_VAL_TYPE_STR) {
/* escape non-printable chars */
p = pkg_realloc(dlg_val_buf, 4 * dv->val.s.len + 1);
if (!p) {
LM_ERR("not enough mem to allocate: %d\n", dv->val.s.len);
if (!pkg_str_extend(&dlg_val_buf, 4 * dv->val.s.len + 1)) {
LM_ERR("not enough mem to allocate: %d\n", 4 * dv->val.s.len + 1);
continue;
}

p = dlg_val_buf.s;
for (i = 0, j = 0; i < dv->val.s.len; i++) {
if (dv->val.s.s[i] < 0x20 || dv->val.s.s[i] >= 0x7F) {
p[j++] = '\\';
Expand Down

0 comments on commit 95a27fe

Please sign in to comment.