]> git.ipfire.org Git - thirdparty/make.git/commitdiff
Ensure variable_buffer is always set.
authorPaul Smith <psmith@gnu.org>
Mon, 15 Mar 2021 05:09:32 +0000 (01:09 -0400)
committerPaul Smith <psmith@gnu.org>
Mon, 15 Mar 2021 05:17:06 +0000 (01:17 -0400)
Initialize the global variable_buffer in main() so that it is never
a null pointer.  Then invoking variable_expand("") is never needed:
simply use the variable_buffer pointer when we want to restart the
variable buffer.  The main point of this simplification is not to
keep a separate pointer to the beginning of the buffer: this is
dangerous because the buffer may be re-allocated.  Instead always
use the variable_buffer pointer itself.

* src/variable.h (initialize_variable_output): Publish.
* src/expand.c (initialize_variable_output): Remove static.
* src/main.c (main): Initialize variable_buffer.
* src/file.c (enter_prereqs): Don't call variable_expand("") and
don't save a separate buffer pointer than might be outdated.
(expand_deps): Ditto.
* src/read.c (record_files): Ditto.
* src/remake.c (library_search): Ditto.

src/expand.c
src/file.c
src/main.c
src/read.c
src/remake.c
src/variable.h

index 0b6bb255f4bcbb455728c60843d0390e9fa76705..6e5d6d85587e0ba2ef86f3cb55e70eaccd9ca9a0 100644 (file)
@@ -71,10 +71,11 @@ variable_buffer_output (char *ptr, const char *string, size_t length)
   return ptr + length;
 }
 
-/* Return a pointer to the beginning of the variable buffer.  */
+/* Return a pointer to the beginning of the variable buffer.
+   This is called from main() and it should never be null afterward.  */
 
-static char *
-initialize_variable_output (void)
+char *
+initialize_variable_output ()
 {
   /* If we don't have a variable output buffer yet, get one.  */
 
@@ -207,7 +208,7 @@ variable_expand_string (char *line, const char *string, size_t length)
   if (length == 0)
     {
       variable_buffer_output (o, "", 1);
-      return (variable_buffer);
+      return variable_buffer;
     }
 
   /* We need a copy of STRING: due to eval, it's possible that it will get
index 913688981f916d1dd25e4326c568b7b2dd36c223..765037507ab6d07481bb589c2a0940b8a3fbc4a6 100644 (file)
@@ -490,7 +490,6 @@ enter_prereqs (struct dep *deps, const char *stem)
   if (stem)
     {
       const char *pattern = "%";
-      char *buffer = variable_expand ("");
       struct dep *dp = deps, *dl = 0;
 
       while (dp != 0)
@@ -510,14 +509,15 @@ enter_prereqs (struct dep *deps, const char *stem)
               if (stem[0] == '\0')
                 {
                   memmove (percent, percent+1, strlen (percent));
-                  o = variable_buffer_output (buffer, nm, strlen (nm) + 1);
+                  o = variable_buffer_output (variable_buffer, nm,
+                                              strlen (nm) + 1);
                 }
               else
-                o = patsubst_expand_pat (buffer, stem, pattern, nm,
+                o = patsubst_expand_pat (variable_buffer, stem, pattern, nm,
                                          pattern+1, percent+1);
 
               /* If the name expanded to the empty string, ignore it.  */
-              if (buffer[0] == '\0')
+              if (variable_buffer[0] == '\0')
                 {
                   struct dep *df = dp;
                   if (dp == deps)
@@ -529,7 +529,8 @@ enter_prereqs (struct dep *deps, const char *stem)
                 }
 
               /* Save the name.  */
-              dp->name = strcache_add_len (buffer, o - buffer);
+              dp->name = strcache_add_len (variable_buffer,
+                                           o - variable_buffer);
             }
           dp->stem = stem;
           dp->staticpattern = 1;
@@ -587,8 +588,7 @@ expand_deps (struct file *f)
          "$*" so they'll expand properly.  */
       if (d->staticpattern)
         {
-          char *o = variable_expand ("");
-          o = subst_expand (o, name, "%", "$*", 1, 2, 0);
+          char *o = subst_expand (variable_buffer, name, "%", "$*", 1, 2, 0);
           *o = '\0';
           free (name);
           d->name = name = xstrdup (variable_buffer);
index 2c202588c6734f881e9f4fb65218c057fbebb275..d16d17a769c4fd06679973b80ee62e87cdd3b553 100644 (file)
@@ -1086,6 +1086,8 @@ main (int argc, char **argv, char **envp)
   no_default_sh_exe = 1;
 #endif
 
+  initialize_variable_output ();
+
   /* Useful for attaching debuggers, etc.  */
   SPIN ("main-entry");
 
index ac4dc8458e73599a9b3f57c8d894f03cedebfef1..c0e3315f4139954bd8ee9c9fc5d3186d42167a57 100644 (file)
@@ -2202,10 +2202,9 @@ record_files (struct nameseq *filenames, int are_also_makes,
       if (pattern)
         {
           static const char *percent = "%";
-          char *buffer = variable_expand ("");
-          char *o = patsubst_expand_pat (buffer, name, pattern, percent,
-                                         pattern_percent+1, percent+1);
-          f->stem = strcache_add_len (buffer, o - buffer);
+          char *o = patsubst_expand_pat (variable_buffer, name, pattern,
+                                         percent, pattern_percent+1, percent+1);
+          f->stem = strcache_add_len (variable_buffer, o - variable_buffer);
           if (this)
             {
               if (! this->need_2nd_expansion)
index beaf7e054ac4a709f1a0b1a17c0884a97bbd3b93..4d41e5d25ce48420845ca51c6c90e1d8e0ab1b70 100644 (file)
@@ -1646,7 +1646,7 @@ library_search (const char *lib, FILE_TIMESTAMP *mtime_ptr)
       static size_t buflen = 0;
       static size_t libdir_maxlen = 0;
       static unsigned int std_dirs = 0;
-      char *libbuf = variable_expand ("");
+      char *libbuf;
 
       /* Expand the pattern using LIB as a replacement.  */
       {
@@ -1663,10 +1663,12 @@ library_search (const char *lib, FILE_TIMESTAMP *mtime_ptr)
             p[len] = c;
             continue;
           }
-        p4 = variable_buffer_output (libbuf, p, p3-p);
+        p4 = variable_buffer_output (variable_buffer, p, p3-p);
         p4 = variable_buffer_output (p4, lib, liblen);
         p4 = variable_buffer_output (p4, p3+1, len - (p3-p));
         p[len] = c;
+
+        libbuf = variable_buffer;
       }
 
       /* Look first for 'libNAME.a' in the current directory.  */
index 23f3282fa80298de891b1c97d520bc364a0dde0f..509777c4ea111dc56357c157a751e5dbdf008650 100644 (file)
@@ -129,6 +129,7 @@ char *allocated_variable_expand_for_file (const char *line, struct file *file);
   allocated_variable_expand_for_file (line, (struct file *) 0)
 char *expand_argument (const char *str, const char *end);
 char *variable_expand_string (char *line, const char *string, size_t length);
+char *initialize_variable_output ();
 void install_variable_buffer (char **bufp, size_t *lenp);
 void restore_variable_buffer (char *buf, size_t len);