Blame SOURCES/make-3.82-copy-on-expand.patch

5589dc
From 2f661dc20617ba6fdeb2d7e243dc898653faafea Mon Sep 17 00:00:00 2001
5589dc
From: Lubomir Rintel <lkundrak@v3.sk>
5589dc
Date: Tue, 26 Apr 2011 21:50:26 +0200
5589dc
Subject: [PATCH] Always copy the string before expanding it
5589dc
5589dc
It might get freed during expansion, e.g. with eval function.
5589dc
A simple reproducer:
5589dc
5589dc
TRUE = $(eval TRUE := true)
5589dc
all:
5589dc
	$(TRUE)
5589dc
---
5589dc
 ChangeLog |    5 +++++
5589dc
 expand.c  |   18 +++++++++---------
5589dc
 2 files changed, 14 insertions(+), 9 deletions(-)
5589dc
5589dc
diff --git a/ChangeLog b/ChangeLog
5589dc
index 91878fb..7519164 100644
5589dc
--- a/ChangeLog
5589dc
+++ b/ChangeLog
5589dc
@@ -1,3 +1,8 @@
5589dc
+2011-04-26  Lubomir Rintel  <lkundrak@v3.sk>
5589dc
+
5589dc
+	* expand.c (variable_expand_string): Always copy the string
5589dc
+	to expand.
5589dc
+
5589dc
 2010-08-13  Paul Smith  <psmith@gnu.org>
5589dc
 
5589dc
 	* NEWS: Accidentally forgot to back out the sorted wildcard
5589dc
diff --git a/expand.c b/expand.c
5589dc
index 2315b06..3e6e346 100644
5589dc
--- a/expand.c
5589dc
+++ b/expand.c
5589dc
@@ -197,7 +197,7 @@ variable_expand_string (char *line, const char *string, long length)
5589dc
 {
5589dc
   struct variable *v;
5589dc
   const char *p, *p1;
5589dc
-  char *abuf = NULL;
5589dc
+  char *abuf;
5589dc
   char *o;
5589dc
   unsigned int line_offset;
5589dc
 
5589dc
@@ -214,14 +214,15 @@ variable_expand_string (char *line, const char *string, long length)
5589dc
 
5589dc
   /* If we want a subset of the string, allocate a temporary buffer for it.
5589dc
      Most of the functions we use here don't work with length limits.  */
5589dc
-  if (length > 0 && string[length] != '\0')
5589dc
+  if (length == -1)
5589dc
     {
5589dc
-      abuf = xmalloc(length+1);
5589dc
-      memcpy(abuf, string, length);
5589dc
-      abuf[length] = '\0';
5589dc
-      string = abuf;
5589dc
+      length = strlen (string);
5589dc
     }
5589dc
-  p = string;
5589dc
+
5589dc
+  abuf = xmalloc(length+1);
5589dc
+  memcpy(abuf, string, length);
5589dc
+  abuf[length] = '\0';
5589dc
+  p = abuf;
5589dc
 
5589dc
   while (1)
5589dc
     {
5589dc
@@ -411,8 +412,7 @@ variable_expand_string (char *line, const char *string, long length)
5589dc
       ++p;
5589dc
     }
5589dc
 
5589dc
-  if (abuf)
5589dc
-    free (abuf);
5589dc
+  free (abuf);
5589dc
 
5589dc
   variable_buffer_output (o, "", 1);
5589dc
   return (variable_buffer + line_offset);
5589dc
-- 
5589dc
1.7.4.1
5589dc