remap: *actually* build, and fix masked logic errors

Well, now it is actually being compiled, and should hopefully work
again...

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin 2024-06-10 21:06:50 -07:00
parent 33ec23c0dc
commit 2c86ff58dc
3 changed files with 60 additions and 59 deletions

View file

@ -181,7 +181,8 @@ PA_ARG_DISABLED([remap],
[AS_IF([test x"$ac_cv_header_regex_h" = xyes], [AS_IF([test x"$ac_cv_header_regex_h" = xyes],
[AC_SEARCH_LIBS(regcomp, [regex rx], [AC_SEARCH_LIBS(regcomp, [regex rx],
[AC_DEFINE([WITH_REGEX], 1, [AC_DEFINE([WITH_REGEX], 1,
[Define if we are compiling with regex filename remapping.])])])]) [Define if we are compiling with regex filename remapping.])
TFTPDOBJS="remap.\$(O) $TFTPOBJS"])])])
TFTPD_LIBS="$LIBS $XTRALIBS" TFTPD_LIBS="$LIBS $XTRALIBS"
LIBS="$common_libs" LIBS="$common_libs"

View file

@ -185,7 +185,7 @@ static int genmatchstring(char **string, const char *pattern,
len = do_genmatchstring(NULL, pattern, ibuf, pmatch, len = do_genmatchstring(NULL, pattern, ibuf, pmatch,
macrosub, start, NULL); macrosub, start, NULL);
*string = buf = tf_malloc(len + 1); *string = buf = tfmalloc(len + 1);
return do_genmatchstring(buf, pattern, ibuf, pmatch, return do_genmatchstring(buf, pattern, ibuf, pmatch,
macrosub, start, nextp); macrosub, start, nextp);
} }
@ -397,7 +397,7 @@ char *rewrite_string(const struct formats *pf,
const struct rule *ruleptr = rules; const struct rule *ruleptr = rules;
regmatch_t pmatch[10]; regmatch_t pmatch[10];
int i; int i;
int len, newlen; int len;
int was_match = 0; int was_match = 0;
int deadman = deadman_max_steps; int deadman = deadman_max_steps;
int matchsense; int matchsense;
@ -429,12 +429,16 @@ char *rewrite_string(const struct formats *pf,
for (i = 0; i < 10; i++) for (i = 0; i < 10; i++)
pmatch[i].rm_so = pmatch[i].rm_eo = -1; pmatch[i].rm_so = pmatch[i].rm_eo = -1;
was_match = 0;
do { do {
if (!deadman--) if (!deadman--)
goto dead; goto dead;
if (regexec(&ruleptr->rx, current, pmatches, pmatch, 0) if (regexec(&ruleptr->rx, current, pmatches, pmatch, 0)
== matchsense) { != matchsense)
break; /* No match, break out of do loop */
/* Match on this rule */ /* Match on this rule */
was_match = 1; was_match = 1;
@ -462,7 +466,7 @@ char *rewrite_string(const struct formats *pf,
if (ruleptr->rule_flags & RULE_SEDG) { if (ruleptr->rule_flags & RULE_SEDG) {
/* sed-style partial-matching global */ /* sed-style partial-matching global */
while (ggoffset < len && while (ggoffset < len &&
regexec(ruleptr->rx, newstr + ggoffset, regexec(&ruleptr->rx, newstr + ggoffset,
pmatches, pmatch, pmatches, pmatch,
ggoffset ? REG_NOTBOL : 0) ggoffset ? REG_NOTBOL : 0)
== matchsense) { == matchsense) {
@ -489,7 +493,7 @@ char *rewrite_string(const struct formats *pf,
was_match = 0; was_match = 0;
break; break;
} }
}
free(current); free(current);
current = newstr; current = newstr;
if (verbosity >= 3) { if (verbosity >= 3) {
@ -506,14 +510,11 @@ char *rewrite_string(const struct formats *pf,
break; break;
} }
} }
} else {
break; /* No match, terminate unconditionally */
}
/* If the rule is (old-style) global, keep going until no match */ /* If the rule is (old-style) global, keep going until no match */
} while ((ruleptr->rule_flags & (RULE_GLOBAL|RULE_SEDG)) == RULE_GLOBAL); } while ((ruleptr->rule_flags & (RULE_GLOBAL|RULE_SEDG)) == RULE_GLOBAL);
if (was_match) { if (!was_match)
was_match = 0; continue; /* Next rule */
if (ruleptr->rule_flags & (RULE_EXIT|RULE_HASFILE)) { if (ruleptr->rule_flags & (RULE_EXIT|RULE_HASFILE)) {
if (verbosity >= 3) { if (verbosity >= 3) {

View file

@ -499,7 +499,6 @@ int main(int argc, char **argv)
break; break;
case OPT_MAP_STEPS: case OPT_MAP_STEPS:
{ {
char *ep;
unsigned long steps = strtoul(optarg, &ep, 0); unsigned long steps = strtoul(optarg, &ep, 0);
if (*optarg && *ep && steps > 0 && steps <= INT_MAX) { if (*optarg && *ep && steps > 0 && steps <= INT_MAX) {
deadman_max_steps = steps; deadman_max_steps = steps;