Skip to content

Commit

Permalink
sh_setmatch: do not allocate a size 0 block (re: f38494e)
Browse files Browse the repository at this point in the history
In init.c:867, calloc(3) may be called with a size of 0 on line 867
if nmatch is zero but v (value) is non-NULL. (This is currently
triggered by the arrays.sh and attributes.sh regression tests.)

As POSIX states: "If the size of the space requested is 0, the
behavior is implementation-defined: either a null pointer shall be
returned, or the behavior shall be as if the size were some
non-zero value, except that the behavior is undefined if the
returned pointer is used to access an object."[*] Either way, the
behaviour is undefined if the returned pointer is dereferenced.

src/cmd/ksh93/sh/init.c: sh_set_match():
- Before calling calloc, return if either nmatch is 0 (there were
  no matches) or v is NULL (there is no value). Do not require both
  conditions to be true to return.

[*] https://pubs.opengroup.org/onlinepubs/9799919799/functions/calloc.html
  • Loading branch information
McDutchie committed Dec 21, 2024
1 parent 7d8aed3 commit 37b0c85
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/cmd/ksh93/sh/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ void sh_setmatch(const char *v, int vsize, int nmatch, int match[], int index)
free(ap);
SH_MATCHNOD->nvalue = NULL;
SH_MATCHNOD->nvfun = NULL;
if(!(mp->nmatch=nmatch) && !v)
if(!(mp->nmatch=nmatch) || !v)
{
sh.subshell = savesub;
return;
Expand Down

0 comments on commit 37b0c85

Please sign in to comment.