Fix static buffer overflow in genname( ) in LLgen

This should fix at least some instances of the "undefined
reference to `LLnc_recover'" error that happens in some
builds (https://github.com/davidgiven/ack/issues/218).

The bug was that genname( ) used a static `namebuf' buffer
and did not properly check for overflow when writing into
it.  The result was that the `non_corr' variable was
sometimes overwritten with a non-zero value when it should
be zero, causing bogus results later.

This proposed patch makes genname( ) dynamically allocate
and resize a buffer for holding a target file name.

I also take this chance to fix a typo in correct_prefix().
This commit is contained in:
Tee-Kiah Chia 2021-03-16 17:59:29 +00:00
parent 7c51d86c4e
commit 9c7ce04cec

View file

@ -1366,15 +1366,15 @@ STATIC void gencases(int *tokenlist, int caseno, int compacted)
} }
} }
static char namebuf[20];
/* /*
* Generate a target file name from the * Generate a target file name from the
* source file name s. * source file name s.
*/STATIC string genname(string s) */
STATIC string genname(string s)
{ {
register string c, d; register string namebuf, c, d;
namebuf = alloc(strlen(s) + 3);
c = namebuf; c = namebuf;
while (*s) while (*s)
{ {
@ -1394,14 +1394,10 @@ static char namebuf[20];
break; break;
if (d == namebuf) if (d == namebuf)
d = c; d = c;
if (d >= &namebuf[12])
{
fatal(0, "%s : filename too long", namebuf);
}
*d++ = '.'; *d++ = '.';
*d++ = 'c'; *d++ = 'c';
*d = '\0'; *d++ = '\0';
return namebuf; return ralloc(namebuf, d - namebuf);
} }
STATIC void genpush(int d) STATIC void genpush(int d)
@ -1509,7 +1505,7 @@ STATIC void correct_prefix(void)
fprintf(f, "#define LLoldlevel %soldlevel\n", s); fprintf(f, "#define LLoldlevel %soldlevel\n", s);
fprintf(f, "#define LLmessage %smessage\n", s); fprintf(f, "#define LLmessage %smessage\n", s);
#ifdef NON_CORRECTING #ifdef NON_CORRECTING
fprintf(f, "#define LLnc_recovery %sncrecovery\n", s); fprintf(f, "#define LLnc_recover %sncrecover\n", s);
fprintf(f, "#define LLstartsymb %sstartsymb\n", s); fprintf(f, "#define LLstartsymb %sstartsymb\n", s);
#endif #endif
} }