Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mishandling of ^ inside an expression #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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