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,67 +429,71 @@ 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)
/* Match on this rule */ break; /* No match, break out of do loop */
was_match = 1;
if (ruleptr->rule_flags & RULE_ABORT) { /* Match on this rule */
if (verbosity >= 3) { was_match = 1;
syslog(LOG_INFO, "remap: rule %d: abort: %s",
ruleptr->nrule, current); if (ruleptr->rule_flags & RULE_ABORT) {
} if (verbosity >= 3) {
if (ruleptr->pattern[0]) { syslog(LOG_INFO, "remap: rule %d: abort: %s",
/* Custom error message */ ruleptr->nrule, current);
genmatchstring(&newstr, ruleptr->pattern, current,
pmatch, macrosub, 0, NULL);
*errmsg = newstr;
} else {
*errmsg = NULL;
}
free(current);
return (NULL);
} }
if (ruleptr->pattern[0]) {
/* Custom error message */
genmatchstring(&newstr, ruleptr->pattern, current,
pmatch, macrosub, 0, NULL);
*errmsg = newstr;
} else {
*errmsg = NULL;
}
free(current);
return (NULL);
}
if (ruleptr->rule_flags & RULE_REWRITE) { if (ruleptr->rule_flags & RULE_REWRITE) {
len = genmatchstring(&newstr, ruleptr->pattern, current, len = genmatchstring(&newstr, ruleptr->pattern, current,
pmatch, macrosub, 0, &ggoffset); pmatch, macrosub, 0, &ggoffset);
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) {
if (!deadman--) { if (!deadman--) {
free(current); free(current);
current = newstr; current = newstr;
goto dead; goto dead;
}
len = genmatchstring(&newerstr, ruleptr->pattern,
newstr, pmatch, macrosub,
ggoffset, &ggoffset);
free(newstr);
newstr = newerstr;
}
}
if ((ruleptr->rule_flags & RULE_HASFILE) &&
pf->f_validate(newstr, mode, pf, &accerr)) {
if (verbosity >= 3) {
syslog(LOG_INFO, "remap: rule %d: ignored rewrite (%s): %s",
ruleptr->nrule, accerr, newstr);
} }
len = genmatchstring(&newerstr, ruleptr->pattern,
newstr, pmatch, macrosub,
ggoffset, &ggoffset);
free(newstr); free(newstr);
was_match = 0; newstr = newerstr;
break;
} }
} }
if ((ruleptr->rule_flags & RULE_HASFILE) &&
pf->f_validate(newstr, mode, pf, &accerr)) {
if (verbosity >= 3) {
syslog(LOG_INFO, "remap: rule %d: ignored rewrite (%s): %s",
ruleptr->nrule, accerr, newstr);
}
free(newstr);
was_match = 0;
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 { /* If the rule is (old-style) global, keep going until no match */
break; /* No match, terminate unconditionally */ } while ((ruleptr->rule_flags & (RULE_GLOBAL|RULE_SEDG)) == RULE_GLOBAL);
}
/* If the rule is (old-style) global, keep going until no match */
} 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;