From e4d5d434247b720a3f78e1f7279168b5e6bf628e Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Mon, 29 Aug 2011 16:20:19 +0000 Subject: 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 --- expand.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) (limited to 'expand.c') diff --git a/expand.c b/expand.c index d1404b9..f5b6b99 100644 --- a/expand.c +++ b/expand.c @@ -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); -- cgit v1.2.3