Skip to content

Commit

Permalink
Cleanups and bugfixes. Leak less memory, pass tests with ASAN=1.
Browse files Browse the repository at this point in the history
  • Loading branch information
landley committed Nov 23, 2024
1 parent 493bd40 commit cfef2aa
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions toys/pending/sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ static char *getvar(char *s)
// Append variable to ff->vars, returning *struct. Does not check duplicates.
static struct sh_vars *addvar(char *s, struct sh_fcall *ff)
{
if (ff->varslen == ff->varscap && !(ff->varslen&31)) {
if (ff->varslen == ff->varscap) {
ff->varscap += 32;
ff->vars = xrealloc(ff->vars, (ff->varscap)*sizeof(*ff->vars));
}
Expand Down Expand Up @@ -847,8 +847,8 @@ static int anystr(char *s, char **try)
// Update $IFS cache in function call stack after variable assignment
static void cache_ifs(char *s, struct sh_fcall *ff)
{
if (!strncmp(s, "IFS=", 4))
do ff->ifs = s+4; while ((ff = ff->next) != TT.ff->prev);
if (strstart(&s, "IFS="))
do ff->ifs = s; while ((ff = ff->next) != TT.ff->prev);
}

// declare -aAilnrux
Expand Down Expand Up @@ -951,7 +951,7 @@ static struct sh_vars *setvar_found(char *s, int freeable, struct sh_vars *var)
}

// Creates new variables (local or global) and handles +=
// returns 0 on error, else sh_vars of new entry.
// returns 0 on error, else sh_vars of new entry. Adds at ff if not found.
static struct sh_vars *setvar_long(char *s, int freeable, struct sh_fcall *ff)
{
struct sh_vars *vv = 0, *was;
Expand Down Expand Up @@ -982,7 +982,7 @@ static struct sh_vars *setvar_long(char *s, int freeable, struct sh_fcall *ff)
// Returns sh_vars * or 0 for failure (readonly, etc)
static struct sh_vars *setvar(char *str)
{
return setvar_long(str, 0, TT.ff->prev);
return setvar_long(str, 1, TT.ff->prev);
}


Expand All @@ -1004,7 +1004,7 @@ static int unsetvar(char *name)
// free from global context
} else {
if (!(var->flags&VAR_NOFREE)) free(var->str);
memmove(var, var+1, sizeof(ff->vars)*(ff->varslen-(var-ff->vars)));
memmove(var, var+1, sizeof(ff->vars)*(ff->varslen-- -(var-ff->vars)));
}
if (!strcmp(name, "IFS"))
do ff->ifs = " \t\n"; while ((ff = ff->next) != TT.ff->prev);
Expand Down

0 comments on commit cfef2aa

Please sign in to comment.