Skip to content

Commit cce085a

Browse files
committed
fix mishandling of ^ inside an expression
1 parent 711981b commit cce085a

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

re.c

+1
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ static int matchone(regex_t p, char c)
393393
case NOT_ALPHA: return !matchalphanum(c);
394394
case WHITESPACE: return matchwhitespace(c);
395395
case NOT_WHITESPACE: return !matchwhitespace(c);
396+
case BEGIN: return 0;
396397
default: return (p.u.ch == c);
397398
}
398399
}

tests/test1.c

+18-2
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,12 @@ int main()
101101
int should_fail;
102102
int length;
103103
int correctlen;
104-
size_t ntests = sizeof(test_vector) / sizeof(*test_vector);
104+
size_t nvector_tests = sizeof(test_vector) / sizeof(*test_vector);
105+
size_t ntests = nvector_tests + 1;
105106
size_t nfailed = 0;
106107
size_t i;
107108

108-
for (i = 0; i < ntests; ++i)
109+
for (i = 0; i < nvector_tests; ++i)
109110
{
110111
pattern = test_vector[i][1];
111112
text = test_vector[i][2];
@@ -141,6 +142,21 @@ int main()
141142
}
142143
}
143144

145+
// regression test for unhandled BEGIN in the middle of an expression
146+
// we need to test text strings with all possible values for the second
147+
// byte because re.c was matching it against an uninitalized value, so
148+
// it could be anything
149+
pattern = "a^";
150+
for (i = 0; i < 255; i++) {
151+
char text_buf[] = { 'a', i, '\0' };
152+
int m = re_match(pattern, text_buf, &length);
153+
if (m != -1) {
154+
fprintf(stderr, "[%lu/%lu]: pattern '%s' matched '%s' unexpectedly", ntests, ntests, pattern, text_buf);
155+
nfailed += 1;
156+
break;
157+
}
158+
}
159+
144160
// printf("\n");
145161
printf("%lu/%lu tests succeeded.\n", ntests - nfailed, ntests);
146162
printf("\n");

0 commit comments

Comments
 (0)