Skip to content

Commit

Permalink
fix mishandling of ^ inside an expression
Browse files Browse the repository at this point in the history
  • Loading branch information
marler8997 committed Mar 8, 2021
1 parent 711981b commit 964b93a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ test: all
@$(PYTHON) ./scripts/regex_test_neg.py [012345-9] $(NRAND_TESTS)
@$(PYTHON) ./scripts/regex_test_neg.py [0-56789] $(NRAND_TESTS)
@$(PYTHON) ./scripts/regex_test_neg.py .*123faerdig $(NRAND_TESTS)
@$(PYTHON) ./scripts/regex_test_neg.py a^ $(NRAND_TESTS)
@echo
@echo
@./tests/test2
Expand Down
1 change: 1 addition & 0 deletions re.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ static int matchone(regex_t p, char c)
case NOT_ALPHA: return !matchalphanum(c);
case WHITESPACE: return matchwhitespace(c);
case NOT_WHITESPACE: return !matchwhitespace(c);
case BEGIN: return 0;
default: return (p.u.ch == c);
}
}
Expand Down
20 changes: 18 additions & 2 deletions tests/test1.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ int main()
int should_fail;
int length;
int correctlen;
size_t ntests = sizeof(test_vector) / sizeof(*test_vector);
size_t nvector_tests = sizeof(test_vector) / sizeof(*test_vector);
size_t ntests = nvector_tests + 1;
size_t nfailed = 0;
size_t i;

for (i = 0; i < ntests; ++i)
for (i = 0; i < nvector_tests; ++i)
{
pattern = test_vector[i][1];
text = test_vector[i][2];
Expand Down Expand Up @@ -141,6 +142,21 @@ int main()
}
}

// regression test for unhandled BEGIN in the middle of an expression
// we need to test text strings with all possible values for the second
// byte because re.c was matching it against an uninitalized value, so
// it could be anything
pattern = "a^";
for (i = 0; i < 255; i++) {
char text_buf[] = { 'a', i, '\0' };
int m = re_match(pattern, text_buf, &length);
if (m != -1) {
fprintf(stderr, "[%lu/%lu]: pattern '%s' matched '%s' unexpectedly", ntests, ntests, pattern, text_buf);
nfailed += 1;
break;
}
}

// printf("\n");
printf("%lu/%lu tests succeeded.\n", ntests - nfailed, ntests);
printf("\n");
Expand Down

0 comments on commit 964b93a

Please sign in to comment.