unifdef: Fix collision check when adding symbols.

findsym() is intended for use while parsing input, so it should not be
called from addsym2() or indirectsym(), which are called before any
input is processed.

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D48733
This commit is contained in:
Dag-Erling Smørgrav
2025-01-30 22:10:29 +01:00
parent ace4637ee0
commit c63af363c2
2 changed files with 15 additions and 6 deletions
+5 -6
View File
@@ -1488,7 +1488,7 @@ findsym(const char **strp)
static void static void
indirectsym(void) indirectsym(void)
{ {
const char *cp; struct macro key = { 0 };
int changed; int changed;
struct macro *sym, *ind; struct macro *sym, *ind;
@@ -1497,10 +1497,9 @@ indirectsym(void)
RB_FOREACH(sym, MACROMAP, &macro_tree) { RB_FOREACH(sym, MACROMAP, &macro_tree) {
if (sym->value == NULL) if (sym->value == NULL)
continue; continue;
cp = sym->value; key.name = sym->value;
ind = findsym(&cp); ind = RB_FIND(MACROMAP, &macro_tree, &key);
if (ind == NULL || ind == sym || if (ind == NULL || ind == sym ||
*cp != '\0' ||
ind->value == NULL || ind->value == NULL ||
ind->value == sym->value) ind->value == sym->value)
continue; continue;
@@ -1539,10 +1538,10 @@ addsym1(bool ignorethis, bool definethis, char *symval)
static void static void
addsym2(bool ignorethis, const char *symname, const char *val) addsym2(bool ignorethis, const char *symname, const char *val)
{ {
const char *cp = symname; struct macro key = { .name = symname };
struct macro *sym, *r; struct macro *sym, *r;
sym = findsym(&cp); sym = RB_FIND(MACROMAP, &macro_tree, &key);
if (sym == NULL) { if (sym == NULL) {
sym = calloc(1, sizeof(*sym)); sym = calloc(1, sizeof(*sym));
sym->ignore = ignorethis; sym->ignore = ignorethis;
+10
View File
@@ -36,7 +36,17 @@ EOF
atf_check -s exit:1 -o inline:"b\n" unifdef -DFOO -DFOO=0 <file atf_check -s exit:1 -o inline:"b\n" unifdef -DFOO -DFOO=0 <file
} }
atf_test_case sDU
sDU_head() {
atf_set descr "simultaneous use of -s and -D or -U"
}
sDU_body() {
atf_check unifdef -s -DFOO -UFOO /dev/null
atf_check unifdef -s -DFOO -DBAR=FOO /dev/null
}
atf_init_test_cases() { atf_init_test_cases() {
atf_add_test_case hash_comment atf_add_test_case hash_comment
atf_add_test_case redefine atf_add_test_case redefine
atf_add_test_case sDU
} }