Skip to content

Commit bbeb38d

Browse files
ronagaduh95
authored andcommitted
buffer: fix end parameter bugs in indexOf/lastIndexOf
- Fix FastIndexOfNumber parameter order mismatch (end_i64 and is_forward were swapped vs the JS call site and slow path) - Clamp negative end values to 0 to prevent size_t overflow in IndexOfString, IndexOfBuffer, and IndexOfNumberImpl - Clamp empty needle result to search_end Signed-off-by: Robert Nagy <ronagy@icloud.com> Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> PR-URL: #62711 Fixes: #62873 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 7a52fd0 commit bbeb38d

2 files changed

Lines changed: 50 additions & 11 deletions

File tree

src/node_buffer.cc

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -983,8 +983,8 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
983983
if (!StringBytes::Size(isolate, needle, enc).To(&needle_length)) return;
984984

985985
// search_end is the exclusive upper bound of the search range.
986-
size_t search_end = static_cast<size_t>(
987-
std::min(end_i64, static_cast<int64_t>(haystack_length)));
986+
size_t search_end = static_cast<size_t>(std::min(
987+
std::max(end_i64, int64_t{0}), static_cast<int64_t>(haystack_length)));
988988
if (enc == UCS2) search_end &= ~static_cast<size_t>(1);
989989

990990
int64_t opt_offset = IndexOfOffset(haystack_length,
@@ -993,8 +993,10 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
993993
is_forward);
994994

995995
if (needle_length == 0) {
996-