- Fix Savannah bug #27093
authorPaul Smith <psmith@gnu.org>
Sun, 2 Aug 2009 16:05:42 +0000 (16:05 +0000)
committerPaul Smith <psmith@gnu.org>
Sun, 2 Aug 2009 16:05:42 +0000 (16:05 +0000)
- Fix Savannah bug #27143
- Fix Savannah bug #23960
- Fix Savannah bug #27148

ChangeLog
doc/make.texi
expand.c
job.c
variable.c

index 3f0b07342f5448102d0340cfb9219857d9735d61..26abd12788c521386530cd927d6067e56b0b3e42 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2009-08-01  Paul Smith  <psmith@gnu.org>
+
+       * expand.c (expand_argument): If the argument is large enough use
+       xmalloc() instead of alloca().  Fixes Savannah bug #27143.
+
+       * variable.c (do_variable_definition): Avoid using alloca() to
+       hold values, which can be large.  Fixes Savannah bug #23960.
+
+       * job.c (new_job): Use memmove() instead of strcpy() since both
+       pointers are in the same memory block.  Fixes Savannah bug #27148.
+       Patch by Petr Machata.
+
 2009-07-29  Ralf Wildenhues  <Ralf.Wildenhues@gmx.de>
 
         * job.c (construct_command_argv_internal): Add "ulimit" and
index c036a6dfd580a0aa6a35d9bf6ed66bf93d0408ce..f73de46ca8c7a5b634fc083fcf65e6f721eda3c0 100644 (file)
@@ -8478,7 +8478,7 @@ for full details on suffix rules.
 @pindex .o
 @pindex .c
 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
-a recipe of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
+a recipe of the form @samp{$(CC) $(CPPFLAGS) $(CFLAGS) -c}.@refill
 
 @item Compiling C++ programs
 @cindex C++, rule to compile
@@ -8488,7 +8488,7 @@ a recipe of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
 @pindex .C
 @file{@var{n}.o} is made automatically from @file{@var{n}.cc},
 @file{@var{n}.cpp}, or @file{@var{n}.C} with a recipe of the form
-@samp{$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)}.  We encourage you to use the
+@samp{$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c}.  We encourage you to use the
 suffix @samp{.cc} for C++ source files instead of @samp{.C}.@refill
 
 @item Compiling Pascal programs
@@ -8496,7 +8496,7 @@ suffix @samp{.cc} for C++ source files instead of @samp{.C}.@refill
 @pindex pc
 @pindex .p
 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
-with the recipe @samp{$(PC) -c $(PFLAGS)}.@refill
+with the recipe @samp{$(PC) $(PFLAGS) -c}.@refill
 
 @item Compiling Fortran and Ratfor programs
 @cindex Fortran, rule to compile
@@ -8511,11 +8511,11 @@ Fortran compiler.  The precise recipe used is as follows:@refill
 
 @table @samp
 @item .f
-@samp{$(FC) -c $(FFLAGS)}.
+@samp{$(FC) $(FFLAGS) -c}.
 @item .F
-@samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
+@samp{$(FC) $(FFLAGS) $(CPPFLAGS) -c}.
 @item .r
-@samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
+@samp{$(FC) $(FFLAGS) $(RFLAGS) -c}.
 @end table
 
 @item Preprocessing Fortran and Ratfor programs
@@ -8526,9 +8526,9 @@ program.  The precise recipe used is as follows:@refill
 
 @table @samp
 @item .F
-@samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
+@samp{$(FC) $(CPPFLAGS) $(FFLAGS) -F}.
 @item .r
-@samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
+@samp{$(FC) $(FFLAGS) $(RFLAGS) -F}.
 @end table
 
 @item Compiling Modula-2 programs
index dc4ea47f331bb28769b521d454cfa56fe5a5c84e..b5d533845578524a79f5a9f1b37bcfa826d51007 100644 (file)
--- a/expand.c
+++ b/expand.c
@@ -438,7 +438,8 @@ variable_expand (const char *line)
 char *
 expand_argument (const char *str, const char *end)
 {
-  char *tmp;
+  char *tmp, *alloc = NULL;
+  char *r;
 
   if (str == end)
     return xstrdup("");
@@ -446,11 +447,20 @@ expand_argument (const char *str, const char *end)
   if (!end || *end == '\0')
     return allocated_variable_expand (str);
 
-  tmp = alloca (end - str + 1);
+  if (end - str + 1 > 1000)
+    tmp = alloc = xmalloc (end - str + 1);
+  else
+    tmp = alloca (end - str + 1);
+
   memcpy (tmp, str, end - str);
   tmp[end - str] = '\0';
 
-  return allocated_variable_expand (tmp);
+  r = allocated_variable_expand (tmp);
+
+  if (alloc)
+    free (alloc);
+
+  return r;
 }
 \f
 /* Expand LINE for FILE.  Error messages refer to the file and line where
@@ -563,11 +573,6 @@ allocated_variable_expand_for_file (const char *line, struct file *file)
 
   value = variable_expand_for_file (line, file);
 
-#if 0
-  /* Waste a little memory and save time.  */
-  value = xrealloc (value, strlen (value))
-#endif
-
   variable_buffer = obuf;
   variable_buffer_length = olen;
 
diff --git a/job.c b/job.c
index b52868f737ae25fb57335314bc06c0516a303344..a4dadb17f6fb57d50966360a6fc9c8fe8ff98132 100644 (file)
--- a/job.c
+++ b/job.c
@@ -1600,7 +1600,7 @@ new_job (struct file *file)
       /* There are no more references in this line to worry about.
         Copy the remaining uninteresting text to the output.  */
       if (out != in)
-       strcpy (out, in);
+       memmove (out, in, strlen (in) + 1);
 
       /* Finally, expand the line.  */
       lines[i] = allocated_variable_expand_for_file (cmds->command_lines[i],
index 92a96ecf0e8cd4b3c1db12fc65b632a7833341a6..10bbc545972cacae04ac1da65bb799dbcd52377b 100644 (file)
@@ -1091,7 +1091,7 @@ do_variable_definition (const struct floc *flocp, const char *varname,
 
             unsigned int oldlen, vallen;
             const char *val;
-            char *tp;
+            char *tp = NULL;
 
             val = value;
             if (v->recursive)
@@ -1104,15 +1104,17 @@ do_variable_definition (const struct floc *flocp, const char *varname,
                  when it was set; and from the expanded new value.  Allocate
                  memory for the expansion as we may still need the rest of the
                  buffer if we're looking at a target-specific variable.  */
-              val = alloc_value = allocated_variable_expand (val);
+              val = tp = allocated_variable_expand (val);
 
             oldlen = strlen (v->value);
             vallen = strlen (val);
-            tp = alloca (oldlen + 1 + vallen + 1);
-            memcpy (tp, v->value, oldlen);
-            tp[oldlen] = ' ';
-            memcpy (&tp[oldlen + 1], val, vallen + 1);
-            p = tp;
+            p = alloc_value = xmalloc (oldlen + 1 + vallen + 1);
+            memcpy (alloc_value, v->value, oldlen);
+            alloc_value[oldlen] = ' ';
+            memcpy (&alloc_value[oldlen + 1], val, vallen + 1);
+
+            if (tp)
+              free (tp);
           }
       }
     }
@@ -1220,10 +1222,10 @@ do_variable_definition (const struct floc *flocp, const char *varname,
         }
       else
         {
-          if (alloc_value)
-            free (alloc_value);
+          char *tp = alloc_value;
 
           alloc_value = allocated_variable_expand (p);
+
           if (find_and_set_default_shell (alloc_value))
             {
               v = define_variable_in_set (varname, strlen (varname), p,
@@ -1236,6 +1238,9 @@ do_variable_definition (const struct floc *flocp, const char *varname,
             }
           else
             v = lookup_variable (varname, strlen (varname));
+
+          if (tp)
+            free (tp);
         }
     }
   else