Skip to content

Commit e7cb583

Browse files
committed
Catch end-of-string in the source string when performing a range match
This commit guards the case for range matches (e.g., "[a-z]") with an end-of-string check for the source string. This prevents an inadvertent interpretation of the nul terminator as a matching character, as well as preventing bugs as in issue #115.
1 parent 27ed71e commit e7cb583

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

match.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ extern bool match(char *p, char *m, char *s) {
2626
continue;
2727
}
2828
break;
29-
case '[': {
30-
int r = 1 + rangematch(p+1, *s);
31-
if (r > 0) {
32-
p += r, m += r, s++;
33-
continue;
29+
case '[':
30+
if (*s) {
31+
int r = 1 + rangematch(p+1, *s);
32+
if (r > 0) {
33+
p += r, m += r, s++;
34+
continue;
35+
}
3436
}
3537
break;
36-
}
3738
case '*':
3839
next.p = p++;
3940
next.m = m++;
@@ -42,7 +43,6 @@ extern bool match(char *p, char *m, char *s) {
4243
default:
4344
panic("bad metacharacter in match");
4445
/* NOTREACHED */
45-
return FALSE; /* hush up gcc -Wall */
4646
}
4747
}
4848
if (next.s != NULL) {

trip.rc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,10 @@ if (~ x [y])
358358
fail rangematch out of range
359359
if (~ x x?)
360360
fail too many characters in pattern
361+
if (~ . .[~.])
362+
fail matched nul terminator
363+
if (~ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab a*a*a*a*a*a*a*a)
364+
fail this should finish in linear time
361365

362366
sh -c 'test -f /////$tmpdir//////a?c.'^$pid || fail glob with many slashes
363367
if (!~ /////$tmpdir//////a*.$pid /////$tmpdir//////a?c.$pid)

0 commit comments

Comments
 (0)