Skip to content

Commit

Permalink
Check length before reading in stringmatchlen
Browse files Browse the repository at this point in the history
When a pattern is not NUL-terminated, stringmatchlen could overrun the
end in some cases.

Signed-off-by: Thalia Archibald <[email protected]>
  • Loading branch information
thaliaarchi committed Dec 12, 2024
1 parent 5f7fe9e commit 44940a5
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 44940a5

Please sign in to comment.