| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7a52fd0 commit bbeb38d
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -983,8 +983,8 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) { | |||
| 983 | 983 | if (!StringBytes::Size(isolate, needle, enc).To(&needle_length)) return; | |
| 984 | 984 | ||
| 985 | 985 | // 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))); | ||
| 988 | 988 | if (enc == UCS2) search_end &= ~static_cast<size_t>(1); | |
| 989 | 989 | ||
| 990 | 990 | int64_t opt_offset = IndexOfOffset(haystack_length, | |
@@ -993,8 +993,10 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) { | |||
| 993 | 993 | is_forward); | |
| 994 | 994 | ||
| 995 | 995 | if (needle_length == 0) { | |
| 996 | - // Match String#indexOf() and String#lastIndexOf() behavior. | ||
| 997 | - args.GetReturnValue().Set(static_cast<double>(opt_offset)); | ||
| 996 | + // Match String#indexOf() and String#lastIndexOf() behavior, | ||
| 997 | + // but clamp to search_end. | ||
| 998 | + int64_t clamped = std::min(opt_offset, static_cast<int64_t>(search_end)); | ||
| 999 | + args.GetReturnValue().Set(static_cast<double>(clamped)); | ||
| 998 | 1000 | return; | |
| 999 | 1001 | } | |
| 1000 | 1002 | ||
@@ -1108,8 +1110,8 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) { | |||
| 1108 | 1110 | const size_t needle_length = needle_contents.length(); | |
| 1109 | 1111 | ||
| 1110 | 1112 | // search_end is the exclusive upper bound of the search range. | |
| 1111 | - size_t search_end = static_cast<size_t>( | ||
| 1112 | - std::min(end_i64, static_cast<int64_t>(haystack_length))); | ||
| 1113 | + size_t search_end = static_cast<size_t>(std::min( | ||
| 1114 | + std::max(end_i64, int64_t{0}), static_cast<int64_t>(haystack_length))); | ||
| 1113 | 1115 | if (enc == UCS2) search_end &= ~static_cast<size_t>(1); | |
| 1114 | 1116 | ||
| 1115 | 1117 | int64_t opt_offset = IndexOfOffset(haystack_length, | |
@@ -1118,8 +1120,10 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) { | |||
| 1118 | 1120 | is_forward); | |
| 1119 | 1121 | ||
| 1120 | 1122 | if (needle_length == 0) { | |
| 1121 | - // Match String#indexOf() and String#lastIndexOf() behavior. | ||
| 1122 | - args.GetReturnValue().Set(static_cast<double>(opt_offset)); | ||
| 1123 | + // Match String#indexOf() and String#lastIndexOf() behavior, | ||
| 1124 | + // but clamp to search_end. | ||
| 1125 | + int64_t clamped = std::min(opt_offset, static_cast<int64_t>(search_end)); | ||
| 1126 | + args.GetReturnValue().Set(static_cast<double>(clamped)); | ||
| 1123 | 1127 | return; | |
| 1124 | 1128 | } | |
| 1125 | 1129 | ||
@@ -1184,8 +1188,8 @@ int32_t IndexOfNumberImpl(Local<Value> buffer_obj, | |||
| 1184 | 1188 | } | |
| 1185 | 1189 | size_t offset = static_cast<size_t>(opt_offset); | |
| 1186 | 1190 | // search_end is the exclusive upper bound of the search range. | |
| 1187 | - size_t search_end = static_cast<size_t>( | ||
| 1188 | - std::min(end_i64, static_cast<int64_t>(buffer_length))); | ||
| 1191 | + size_t search_end = static_cast<size_t>(std::min( | ||
| 1192 | + std::max(end_i64, int64_t{0}), static_cast<int64_t>(buffer_length))); | ||
| 1189 | 1193 | ||
| 1190 | 1194 | const void* ptr; | |
| 1191 | 1195 | if (is_forward) { | |
@@ -1222,8 +1226,8 @@ int32_t FastIndexOfNumber(Local<Value>, | |||
| 1222 | 1226 | Local<Value> buffer_obj, | |
| 1223 | 1227 | uint32_t needle, | |
| 1224 | 1228 | int64_t offset_i64, | |
| 1225 | - int64_t end_i64, | ||
| 1226 | 1229 | bool is_forward, | |
| 1230 | + int64_t end_i64, | ||
| 1227 | 1231 | // NOLINTNEXTLINE(runtime/references) | |
| 1228 | 1232 | FastApiCallbackOptions& options) { | |
| 1229 | 1233 | HandleScope scope(options.isolate); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -691,3 +691,38 @@ assert.strictEqual(reallyLong.lastIndexOf(pattern), 0); | |||
| 691 | 691 | ||
| 692 | 692 | assert.strictEqual(buf.includes('c'), true); | |
| 693 | 693 | } | |
| 694 | + | ||
| 695 | + { | ||
| 696 | + const buf = Buffer.from('abcabc'); | ||
| 697 | + | ||
| 698 | + // Negative end should be treated as 0 (no match possible). | ||
| 699 | + assert.strictEqual(buf.indexOf('a', 0, -1), -1); | ||
| 700 | + assert.strictEqual(buf.indexOf('a', 0, -100), -1); | ||
| 701 | + assert.strictEqual(buf.indexOf(0x61, 0, -1), -1); | ||
| 702 | + assert.strictEqual(buf.lastIndexOf('a', 5, -1), -1); | ||
| 703 | + assert.strictEqual(buf.lastIndexOf(0x61, 5, -1), -1); | ||
| 704 | + assert.strictEqual(buf.includes('a', 0, -1), false); | ||
| 705 | + assert.strictEqual(buf.indexOf(Buffer.from('a'), 0, -1), -1); | ||
| 706 | + assert.strictEqual(buf.lastIndexOf(Buffer.from('a'), 5, -1), -1); | ||
| 707 | + | ||
| 708 | + // End = 0 means empty search range. | ||
| 709 | + assert.strictEqual(buf.indexOf('a', 0, 0), -1); | ||
| 710 | + assert.strictEqual(buf.indexOf(0x61, 0, 0), -1); | ||
| 711 | + assert.strictEqual(buf.lastIndexOf('a', 5, 0), -1); | ||
| 712 | + assert.strictEqual(buf.lastIndexOf(0x61, 5, 0), -1); | ||
| 713 | + | ||
| 714 | + // End greater than buffer length should be clamped. | ||
| 715 | + assert.strictEqual(buf.indexOf('c', 0, 100), 2); | ||
| 716 | + assert.strictEqual(buf.indexOf(0x63, 0, 100), 2); | ||
| 717 | + assert.strictEqual(buf.lastIndexOf('c', 5, 100), 5); | ||
| 718 | + assert.strictEqual(buf.lastIndexOf(0x63, 5, 100), 5); | ||
| 719 | + assert.strictEqual(buf.indexOf(Buffer.from('c'), 0, 100), 2); | ||
| 720 | + | ||
| 721 | + // Empty needle with end parameter should clamp to search_end. | ||
| 722 | + assert.strictEqual(buf.indexOf('', 0, 3), 0); | ||
| 723 | + assert.strictEqual(buf.indexOf('', 5, 3), 3); | ||
| 724 | + assert.strictEqual(buf.indexOf(Buffer.from(''), 5, 3), 3); | ||
| 725 | + assert.strictEqual(buf.indexOf('', 0, 0), 0); | ||
| 726 | + assert.strictEqual(buf.lastIndexOf('', 5, 3), 3); | ||
| 727 | + assert.strictEqual(buf.lastIndexOf(Buffer.from(''), 5, 3), 3); | ||
| 728 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments