From e7cb583f057deb607be97f4aa209270365fee024 Mon Sep 17 00:00:00 2001 From: Byron Rakitzis Date: Sat, 17 May 2025 16:41:25 -0700 Subject: [PATCH] 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. --- match.c | 14 +++++++------- trip.rc | 4 ++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/match.c b/match.c index bcdca3ed..c55c76cd 100644 --- a/match.c +++ b/match.c @@ -26,14 +26,15 @@ extern bool match(char *p, char *m, char *s) { continue; } break; - case '[': { - int r = 1 + rangematch(p+1, *s); - if (r > 0) { - p += r, m += r, s++; - continue; + case '[': + if (*s) { + int r = 1 + rangematch(p+1, *s); + if (r > 0) { + p += r, m += r, s++; + continue; + } } break; - } case '*': next.p = p++; next.m = m++; @@ -42,7 +43,6 @@ extern bool match(char *p, char *m, char *s) { default: panic("bad metacharacter in match"); /* NOTREACHED */ - return FALSE; /* hush up gcc -Wall */ } } if (next.s != NULL) { diff --git a/trip.rc b/trip.rc index 130b96d3..104504e0 100644 --- a/trip.rc +++ b/trip.rc @@ -358,6 +358,10 @@ if (~ x [y]) fail rangematch out of range if (~ x x?) fail too many characters in pattern +if (~ . .[~.]) + fail matched nul terminator +if (~ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab a*a*a*a*a*a*a*a) + fail this should finish in linear time sh -c 'test -f /////$tmpdir//////a?c.'^$pid || fail glob with many slashes if (!~ /////$tmpdir//////a*.$pid /////$tmpdir//////a?c.$pid)