checkpoint on string realloc bug fix attempt

In preparation to fix issue #47, we:

Fix a minor typo in `Makefile.config`.

Move `stringindex(char *format, char *test)` from `str.c` into `codegen.c`
as static function.

Expand `STR_TABLECHUNK` from `1<<10` to `1<<16`.

Identify with an XXX comment, a problematic `realloc()` call in the
`addstr(STRINGHEAD *hp, char *str)` function of `str.c`.

Move the `charstr(int ch)` in `str.c` above the only function that uses
it, `addliteral(char *str)`, and make it a static function.

Improved comments about return values for `str.c` functions that
work on STRINGHEAD pointers.
This commit is contained in:
Landon Curt Noll
2025-08-17 02:18:10 -07:00
parent 077ba65285
commit 95793c3150
4 changed files with 77 additions and 63 deletions

View File

@@ -1,7 +1,7 @@
/*
* codegen - module to generate opcodes from the input tokens
*
* Copyright (C) 1999-2007,2017,2021-2023 David I. Bell and Ernest Bowen
* Copyright (C) 1999-2007,2017,2021-2023,2025 David I. Bell and Ernest Bowen
*
* Primary author: David I. Bell
*
@@ -2425,6 +2425,40 @@ getfilename(char *name, size_t namelen, bool *once)
}
/*
* Useful routine to return the index of one string within another one
* which has the format: "str1\000str2\000str3\000...strn\0\0". Index starts
* at one for the first string. Returns zero if the string being checked
* is not contained in the formatted string.
*
* Be sure to use \000 instead of \0. ANSI-C compilers interpret "foo\0foo..."
* as "foo\017oo...".
*
* given:
* format string formatted into substrings
* test string to be found in formatted string
*/
S_FUNC long
stringindex(char *format, char *test)
{
long index; /* found index */
size_t len; /* length of current piece of string */
size_t testlen; /* length of test string */
testlen = strlen(test);
index = 1;
while (*format) {
len = strlen(format);
if ((len == testlen) && (*format == *test) &&
(strcmp(format, test) == 0))
return index;
format += (len + 1);
index++;
}
return 0;
}
/*
* Read the show command to display useful information
*/