mirror of
https://git.savannah.gnu.org/git/make.git
synced 2025-02-06 22:45:27 +00:00
Fix memory leak during environment option decoding.
* main.c (decode_switches): Always make a copy of option arguments. (decode_env_switches): Use a stack buffer to convert environment switches for parsing.
This commit is contained in:
parent
12a3104c3d
commit
f8905059c3
1 changed files with 11 additions and 10 deletions
21
main.c
21
main.c
|
@ -2814,7 +2814,7 @@ decode_switches (int argc, char **argv, int env)
|
||||||
if (cs->type == filename)
|
if (cs->type == filename)
|
||||||
sl->list[sl->idx++] = expand_command_line_file (optarg);
|
sl->list[sl->idx++] = expand_command_line_file (optarg);
|
||||||
else
|
else
|
||||||
sl->list[sl->idx++] = optarg;
|
sl->list[sl->idx++] = xstrdup (optarg);
|
||||||
sl->list[sl->idx] = 0;
|
sl->list[sl->idx] = 0;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -2904,7 +2904,7 @@ static void
|
||||||
decode_env_switches (char *envar, unsigned int len)
|
decode_env_switches (char *envar, unsigned int len)
|
||||||
{
|
{
|
||||||
char *varref = alloca (2 + len + 2);
|
char *varref = alloca (2 + len + 2);
|
||||||
char *value, *p;
|
char *value, *p, *buf;
|
||||||
int argc;
|
int argc;
|
||||||
char **argv;
|
char **argv;
|
||||||
|
|
||||||
|
@ -2925,15 +2925,18 @@ decode_env_switches (char *envar, unsigned int len)
|
||||||
/* Allocate a vector that is definitely big enough. */
|
/* Allocate a vector that is definitely big enough. */
|
||||||
argv = alloca ((1 + len + 1) * sizeof (char *));
|
argv = alloca ((1 + len + 1) * sizeof (char *));
|
||||||
|
|
||||||
/* Allocate a buffer to copy the value into while we split it into words
|
/* We need a buffer to copy the value into while we split it into words
|
||||||
and unquote it. We must use permanent storage for this because
|
and unquote it. */
|
||||||
decode_switches may store pointers into the passed argument words. */
|
buf = alloca (2 * len);
|
||||||
p = xmalloc (2 * len);
|
|
||||||
|
|
||||||
/* getopt will look at the arguments starting at ARGV[1].
|
/* getopt will look at the arguments starting at ARGV[1].
|
||||||
Prepend a spacer word. */
|
Prepend a spacer word. */
|
||||||
argv[0] = 0;
|
argv[0] = 0;
|
||||||
argc = 1;
|
argc = 1;
|
||||||
|
|
||||||
|
/* Set up in case we need to prepend a dash later. */
|
||||||
|
buf[0] = '-';
|
||||||
|
p = buf+1;
|
||||||
argv[argc] = p;
|
argv[argc] = p;
|
||||||
while (*value != '\0')
|
while (*value != '\0')
|
||||||
{
|
{
|
||||||
|
@ -2956,10 +2959,8 @@ decode_env_switches (char *envar, unsigned int len)
|
||||||
|
|
||||||
if (argv[1][0] != '-' && strchr (argv[1], '=') == 0)
|
if (argv[1][0] != '-' && strchr (argv[1], '=') == 0)
|
||||||
/* The first word doesn't start with a dash and isn't a variable
|
/* The first word doesn't start with a dash and isn't a variable
|
||||||
definition. Add a dash and pass it along to decode_switches. We
|
definition, so add a dash. */
|
||||||
need permanent storage for this in case decode_switches saves
|
argv[1] = buf;
|
||||||
pointers into the value. */
|
|
||||||
argv[1] = xstrdup (concat (2, "-", argv[1]));
|
|
||||||
|
|
||||||
/* Parse those words. */
|
/* Parse those words. */
|
||||||
decode_switches (argc, argv, 1);
|
decode_switches (argc, argv, 1);
|
||||||
|
|
Loading…
Reference in a new issue