- Memory cleanups, found with valgrind.
authorPaul Smith <psmith@gnu.org>
Mon, 20 Feb 2006 02:14:00 +0000 (02:14 +0000)
committerPaul Smith <psmith@gnu.org>
Mon, 20 Feb 2006 02:14:00 +0000 (02:14 +0000)
- Fix handling of special targets like .SUFFIX for VMS insensitive targets.
- Don't make temporary batch files for -n.  Make sure batch files are created
  in text mode.

ChangeLog
commands.c
expand.c
file.c
implicit.c
job.c

index 07d1e52f82f85f722c954c2a977d67d9dbb36eba..969df369bca47b86334f75d66a5896fb9b714460 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,25 @@
+2006-02-19  Paul D. Smith  <psmith@gnu.org>
+
+       * commands.c (set_file_variables): Realloc, not malloc, the static
+       string values to avoid memory leaks.
+
+       * expand.c (recursively_expand_for_file): Only set reading_file to
+       an initialized value.
+
+       * implicit.c (pattern_search): We need to make a copy of the stem
+       if we get it from an intermediate dep, since those get freed.
+
+       * file.c (lookup_file) [VMS]: Don't lowercase special targets that
+       begin with ".".
+       (enter_file) [VMS]: Ditto.
+       Patch provided by Hartmut Becker <Hartmut.Becker@hp.com>.
+
+2006-02-18  Eli Zaretskii  <eliz@gnu.org>
+
+        * job.c (construct_command_argv_internal): Don't create a temporary
+       script/batch file if we are under -n.  Call _setmode to switch the
+       script file stream to text mode.
+
 2006-02-17  Paul D. Smith  <psmith@gnu.org>
 
        * variable.c (merge_variable_set_lists): Don't try to merge the
index 691eb35014c9b8d98b75bed3fa7659749a84fdeb..a0a7c5cf9db379f5311c52cea6f8f37cc93d5a8f 100644 (file)
@@ -157,7 +157,7 @@ set_file_variables (struct file *file)
       plus_len++;
 
     if (plus_len > plus_max)
-      plus_value = (char *) xmalloc (plus_max = plus_len);
+      plus_value = xrealloc (plus_value, plus_max = plus_len);
     cp = plus_value;
 
     qmark_len = plus_len + 1;  /* Will be this or less.  */
@@ -206,11 +206,11 @@ set_file_variables (struct file *file)
     cp = caret_value = plus_value; /* Reuse the buffer; it's big enough.  */
 
     if (qmark_len > qmark_max)
-      qmark_value = (char *) xmalloc (qmark_max = qmark_len);
+      qmark_value = xrealloc (qmark_value, qmark_max = qmark_len);
     qp = qmark_value;
 
     if (bar_len > bar_max)
-      bar_value = (char *) xmalloc (bar_max = bar_len);
+      bar_value = xrealloc (bar_value, bar_max = bar_len);
     bp = bar_value;
 
     for (d = file->deps; d != 0; d = d->next)
index d25afef7a808b17410847c963cedb8b697a4600b..39f0d194a88aa3cfb0fc600a47fd43b04f7ca1f1 100644 (file)
--- a/expand.c
+++ b/expand.c
@@ -113,11 +113,18 @@ recursively_expand_for_file (struct variable *v, struct file *file)
       expanding_var = &this_var;
     }
 
+  /* If we have no other file-reading context, use the variable's context. */
+  if (!reading_file)
+    {
+      set_reading = 1;
+      reading_file = &v->fileinfo;
+    }
+
   if (v->expanding)
     {
       if (!v->exp_count)
         /* Expanding V causes infinite recursion.  Lose.  */
-        fatal (this_var,
+        fatal (*expanding_var,
                _("Recursive variable `%s' references itself (eventually)"),
                v->name);
       --v->exp_count;
@@ -129,13 +136,6 @@ recursively_expand_for_file (struct variable *v, struct file *file)
       current_variable_set_list = file->variables;
     }
 
-  /* If we have no other file-reading context, use the variable's context. */
-  if (!reading_file)
-    {
-      set_reading = 1;
-      reading_file = this_var;
-    }
-
   v->expanding = 1;
   if (v->append)
     value = allocated_variable_append (v);
diff --git a/file.c b/file.c
index 0d5e9da50f21f98daaf0f49df0fae7f3eb9a5dee..a0e82228b3adabe80ce16f0e8dd82229bc38a042 100644 (file)
--- a/file.c
+++ b/file.c
@@ -88,14 +88,15 @@ lookup_file (char *name)
      on the command line.  */
 #ifdef VMS
 # ifndef WANT_CASE_SENSITIVE_TARGETS
-  {
-    register char *n;
-    lname = (char *) malloc (strlen (name) + 1);
-    for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
-      *ln = isupper ((unsigned char)*n) ? tolower ((unsigned char)*n) : *n;
-    *ln = '\0';
-    name = lname;
-  }
+  if (*name != '.')
+    {
+      register char *n;
+      lname = (char *) malloc (strlen (name) + 1);
+      for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
+        *ln = isupper ((unsigned char)*n) ? tolower ((unsigned char)*n) : *n;
+      *ln = '\0';
+      name = lname;
+    }
 # endif
 
   while (name[0] == '[' && name[1] == ']' && name[2] != '\0')
@@ -124,7 +125,8 @@ lookup_file (char *name)
   file_key.hname = name;
   f = (struct file *) hash_find_item (&files, &file_key);
 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
-  free (lname);
+  if (*name != '.')
+    free (lname);
 #endif
   return f;
 }
@@ -143,22 +145,23 @@ enter_file (char *name)
   assert (*name != '\0');
 
 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
-  {
-    register char *n;
-    lname = (char *) malloc (strlen (name) + 1);
-    for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
-      {
-        if (isupper ((unsigned char)*n))
-          *ln = tolower ((unsigned char)*n);
-        else
-          *ln = *n;
-      }
+  if (*name != '.')
+    {
+      register char *n;
+      lname = (char *) malloc (strlen (name) + 1);
+      for (n = name, ln = lname; *n != '\0'; ++n, ++ln)
+        {
+          if (isupper ((unsigned char)*n))
+            *ln = tolower ((unsigned char)*n);
+          else
+            *ln = *n;
+        }
 
-    *ln = 0;
-    /* Creates a possible leak, old value of name is unreachable, but I
-       currently don't know how to fix it. */
-    name = lname;
-  }
+      *ln = 0;
+      /* Creates a possible leak, old value of name is unreachable, but I
+         currently don't know how to fix it. */
+      name = lname;
+    }
 #endif
 
   file_key.hname = name;
@@ -167,7 +170,8 @@ enter_file (char *name)
   if (! HASH_VACANT (f) && !f->double_colon)
     {
 #if defined(VMS) && !defined(WANT_CASE_SENSITIVE_TARGETS)
-      free(lname);
+      if (*name != '.')
+        free (lname);
 #endif
       return f;
     }
index d7c140a431d5105a6e42a3c89ee3932275297a0a..054b71a50d098124529c585900ce3a45cbe104bd 100644 (file)
@@ -90,14 +90,9 @@ free_idep_chain (struct idep *p)
         free (p->name);
 
       f = p->intermediate_file;
-      if (f != 0)
-        {
-          /*          if (f->variables)
-                      free_variable_set (f->variables); */
-          if (f->stem < f->name
-              || f->stem > f->name + strlen (f->name))
-            free (f->stem);
-        }
+      if (f != 0
+          && (f->stem < f->name || f->stem > f->name + strlen (f->name)))
+        free (f->stem);
 
       free (p);
     }
@@ -841,7 +836,7 @@ pattern_search (struct file *file, int archive,
 
          f->deps = imf->deps;
          f->cmds = imf->cmds;
-         f->stem = imf->stem;
+         f->stem = xstrdup (imf->stem);
           f->also_make = imf->also_make;
           f->is_target = 1;
 
diff --git a/job.c b/job.c
index 34f359d83ab6063c068b43bd911c08bd9e29dafc..3ef4c7523b5c70fda5ec74d2d1507d851e2ef4d8 100644 (file)
--- a/job.c
+++ b/job.c
@@ -2770,6 +2770,8 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
     /* Some shells do not work well when invoked as 'sh -c xxx' to run a
        command line (e.g. Cygnus GNUWIN32 sh.exe on WIN32 systems).  In these
        cases, run commands via a script file.  */
+    if (just_print_flag)
+      ; /* Do nothing here.  */
     if ((no_default_sh_exe || batch_mode_shell) && batch_filename_ptr) {
       int temp_fd;
       FILE* batch = NULL;
@@ -2784,8 +2786,9 @@ construct_command_argv_internal (char *line, char **restp, char *shell,
                     *batch_filename_ptr));
 
       /* Create a FILE object for the batch file, and write to it the
-        commands to be executed.  */
-      batch = _fdopen (temp_fd, "w");
+        commands to be executed.  Put the batch file in TEXT mode.  */
+      _setmode (temp_fd, _O_TEXT);
+      batch = _fdopen (temp_fd, "wt");
       if (!unixy_shell)
         fputs ("@echo off\n", batch);
       fputs (command_ptr, batch);