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.
This commit is contained in:
Paul Smith 2021-03-15 01:09:32 -04:00
parent c66ec5fa20
commit 52056d7b2c
6 changed files with 22 additions and 17 deletions

View 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

View 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);

View 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");

View 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)

View 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. */

View 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);