diff options
author | Paul Smith <psmith@gnu.org> | 2011-08-29 16:20:19 +0000 |
---|---|---|
committer | Paul Smith <psmith@gnu.org> | 2011-08-29 16:20:19 +0000 |
commit | f72b8a298ebababd509ff4a0b6ea73a7d1d238bc (patch) | |
tree | e3743fa9a60722d0b22116d8a170d376fa0b251e /expand.c | |
parent | 67dd56050ce098744ba739937278ae6c8b55b390 (diff) | |
download | make-f72b8a298ebababd509ff4a0b6ea73a7d1d238bc.tar.gz |
Save strings we're expanding in case an embedded eval causes them
to be freed (if they're the value of a variable that's reset for example).
See Savannah patch #7534
Diffstat (limited to 'expand.c')
-rw-r--r-- | expand.c | 20 |
1 files changed, 7 insertions, 13 deletions
@@ -197,7 +197,7 @@ variable_expand_string (char *line, const char *string, long length) { struct variable *v; const char *p, *p1; - char *abuf = NULL; + char *save; char *o; unsigned int line_offset; @@ -212,16 +212,11 @@ variable_expand_string (char *line, const char *string, long length) return (variable_buffer); } - /* If we want a subset of the string, allocate a temporary buffer for it. - Most of the functions we use here don't work with length limits. */ - if (length > 0 && string[length] != '\0') - { - abuf = xmalloc(length+1); - memcpy(abuf, string, length); - abuf[length] = '\0'; - string = abuf; - } - p = string; + /* We need a copy of STRING: due to eval, it's possible that it will get + freed as we process it (it might be the value of a variable that's reset + for example). Also having a nil-terminated string is handy. */ + save = length < 0 ? xstrdup (string) : xstrndup (string, length); + p = save; while (1) { @@ -411,8 +406,7 @@ variable_expand_string (char *line, const char *string, long length) ++p; } - if (abuf) - free (abuf); + free (save); variable_buffer_output (o, "", 1); return (variable_buffer + line_offset); |