Skip to content

Commit

Permalink
Merge pull request #145 from flaviojs/fix-memory-leak-rommon_var_add
Browse files Browse the repository at this point in the history
Fix memory leak in rommon_var_add
  • Loading branch information
grossmj authored Mar 9, 2024
2 parents 5b1d786 + 15442b7 commit 0af6c74
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions common/rommon_var.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ static struct rommon_var *rommon_var_create(char *name)
return var;
}

/* Delete a variable */
static struct rommon_var *rommon_var_delete(struct rommon_var *var)
{
struct rommon_var *next_var;

next_var = var->next;
free(var->value);
free(var->name);
free(var);
return(next_var);
}

/* Set value for a variable */
static int rommon_var_set(struct rommon_var *var,char *value)
{
Expand All @@ -116,8 +128,10 @@ int rommon_var_add(struct rommon_var_list *rvl,char *name,char *value)
var = rommon_var_create(name);
if (!var) return(-1);

if (rommon_var_set(var,value) == -1)
if (rommon_var_set(var,value) == -1) {
rommon_var_delete(var);
return(-1);
}

var->next = rvl->var_list;
rvl->var_list = var;
Expand Down Expand Up @@ -171,10 +185,7 @@ void rommon_var_clear(struct rommon_var_list *rvl)
return;

for (var = rvl->var_list; var; var = next_var) {
next_var = var->next;
free(var->value);
free(var->name);
free(var);
next_var = rommon_var_delete(var);
}
rvl->var_list = NULL;
}

0 comments on commit 0af6c74

Please sign in to comment.