Skip to content

Commit

Permalink
Check length before reading in stringmatchlen (valkey-io#1431)
Browse files Browse the repository at this point in the history
Fixes four cases where `stringmatchlen` could overrun the pattern if it
is not terminated with NUL.

These commits are cherry-picked from my
[fork](https://github.com/thaliaarchi/antirez-stringmatch) which
extracts `stringmatch` as a library and compares it to other projects by
antirez which use the same matcher.

Signed-off-by: Thalia Archibald <[email protected]>
  • Loading branch information
thaliaarchi authored Dec 13, 2024
1 parent 32f2c73 commit b60097b
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,23 @@ static int stringmatchlen_impl(const char *pattern,

pattern++;
patternLen--;
not_op = pattern[0] == '^';
not_op = patternLen && pattern[0] == '^';
if (not_op) {
pattern++;
patternLen--;
}
match = 0;
while (1) {
if (pattern[0] == '\\' && patternLen >= 2) {
if (patternLen >= 2 && pattern[0] == '\\') {
pattern++;
patternLen--;
if (pattern[0] == string[0]) match = 1;
} else if (pattern[0] == ']') {
break;
} else if (patternLen == 0) {
pattern--;
patternLen++;
break;
} else if (pattern[0] == ']') {
break;
} else if (patternLen >= 3 && pattern[1] == '-') {
int start = pattern[0];
int end = pattern[2];
Expand Down Expand Up @@ -173,7 +173,7 @@ static int stringmatchlen_impl(const char *pattern,
pattern++;
patternLen--;
if (stringLen == 0) {
while (*pattern == '*') {
while (patternLen && *pattern == '*') {
pattern++;
patternLen--;
}
Expand Down

0 comments on commit b60097b

Please sign in to comment.