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

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