Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expr: fix memory management #148

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions toys/pending/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ config EXPR
GLOBALS(
char **tok; // current token, not on the stack since recursive calls mutate it

char *refree;
struct arg_list *allocated; // list of strings allocated during evaluation
)

// Scalar value. If s != NULL, it's a string, otherwise it's an int.
Expand All @@ -68,6 +68,15 @@ struct value {
long long i;
};

// Keep track of an allocated string.
void track_str(char* str)
{
struct arg_list *node = xmalloc(sizeof(struct arg_list));
node->arg = str;
node->next = TT.allocated;
TT.allocated = node;
}

// Get the value as a string.
char *get_str(struct value *v)
{
Expand Down Expand Up @@ -115,8 +124,7 @@ static void re(char *target, char *pattern, struct value *ret)
if (pat.re_nsub>0) {
ret->s = xmprintf("%.*s", (int)(m[1].rm_eo-m[1].rm_so),
target+m[1].rm_so);
if (TT.refree) free(TT.refree);
TT.refree = ret->s;
track_str(ret->s); // free it later
} else assign_int(ret, m[0].rm_eo);
} else {
if (pat.re_nsub>0) ret->s = "";
Expand Down Expand Up @@ -257,5 +265,6 @@ void expr_main(void)

toys.exitval = is_false(&ret);

if (TT.refree) free(TT.refree);
// Free all the strings we allocated.
llist_traverse(TT.allocated, llist_free_arg);
}