Berry fix potential pointer underflow with string.endswith (#23496)

This commit is contained in:
s-hadinger 2025-05-31 20:02:40 +02:00 committed by GitHub
parent 1cd4e27123
commit ea9a24e76d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 8 deletions

View File

@ -29,6 +29,7 @@ All notable changes to this project will be documented in this file.
- DNS setting with `IPAddress4/5` not persisted (#23426)
- Berry avoid json parsing for unmatched commands
- Berry fix integer and real parser to handle overflows
- Berry fix potential pointer underflow with `string.endswith`
### Removed

View File

@ -1074,14 +1074,17 @@ static int str_endswith(bvm *vm)
bbool result = bfalse;
const char *s = be_tostring(vm, 1);
const char *p = be_tostring(vm, 2);
size_t len = (size_t)be_strlen(vm, 2);
if (case_insensitive) {
if (str_strncasecmp(s + (int)strlen(s) - (int)len, p, len) == 0) {
result = btrue;
}
} else {
if (strncmp(s + (int)strlen(s) - (int)len, p, len) == 0) {
result = btrue;
size_t len_s = (size_t)be_strlen(vm, 1);
size_t len_p = (size_t)be_strlen(vm, 2);
if (len_s >= len_p) {
if (case_insensitive) {
if (str_strncasecmp(s + (int)len_s - (int)len_p, p, len_p) == 0) {
result = btrue;
}
} else {
if (strncmp(s + (int)len_s - (int)len_p, p, len_p) == 0) {
result = btrue;
}
}
}
be_pushbool(vm, result);