From: Paul Smith Date: Mon, 15 Mar 2021 05:09:32 +0000 (-0400) Subject: Ensure variable_buffer is always set. X-Git-Tag: 4.3.90~183 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=52056d7b2cb5c3c889096d2138b5b19124cdf6b0;p=thirdparty%2Fmake.git Ensure variable_buffer is always set. 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. --- diff --git a/src/expand.c b/src/expand.c index 0b6bb255..6e5d6d85 100644 --- a/src/expand.c +++ b/src/expand.c @@ -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 diff --git a/src/file.c b/src/file.c index 91368898..76503750 100644 --- a/src/file.c +++ b/src/file.c @@ -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); diff --git a/src/main.c b/src/main.c index 2c202588..d16d17a7 100644 --- a/src/main.c +++ b/src/main.c @@ -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"); diff --git a/src/read.c b/src/read.c index ac4dc845..c0e3315f 100644 --- a/src/read.c +++ b/src/read.c @@ -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) diff --git a/src/remake.c b/src/remake.c index beaf7e05..4d41e5d2 100644 --- a/src/remake.c +++ b/src/remake.c @@ -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. */ diff --git a/src/variable.h b/src/variable.h index 23f3282f..509777c4 100644 --- a/src/variable.h +++ b/src/variable.h @@ -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);