GeezerVison is showing github.com — buffer: fix needle length misestimation for UCS2 · nodejs/node@12a9699
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/node_buffer.cc +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -827,9 +827,9 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) { |
827 | 827 | Local<String> needle = args[1].As<String>(); |
828 | 828 | const char* haystack = ts_obj_data; |
829 | 829 | const size_t haystack_length = ts_obj_length; |
830 | | - // Extended latin-1 characters are 2 bytes in Utf8. |
| 830 | + |
831 | 831 | const size_t needle_length = |
832 | | - enc == BINARY ? needle->Length() : needle->Utf8Length(); |
| 832 | + StringBytes::Size(args.GetIsolate(), needle, enc); |
833 | 833 |
|
834 | 834 |
|
835 | 835 | if (needle_length == 0 || haystack_length == 0) { |
|
Collapse file
Copy file name to clipboardExpand all lines: test/parallel/test-buffer-indexof.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -233,6 +233,12 @@ var allCharsBufferUcs2 = new Buffer(allCharsString, 'ucs2'); |
233 | 233 | assert.equal(-1, allCharsBufferUtf8.indexOf('notfound')); |
234 | 234 | assert.equal(-1, allCharsBufferUcs2.indexOf('notfound')); |
235 | 235 |
|
| 236 | +// Needle is longer than haystack, but only because it's encoded as UTF-16 |
| 237 | +assert.strictEqual(new Buffer('aaaa').indexOf('a'.repeat(4), 'ucs2'), -1); |
| 238 | + |
| 239 | +assert.strictEqual(new Buffer('aaaa').indexOf('a'.repeat(4), 'utf8'), 0); |
| 240 | +assert.strictEqual(new Buffer('aaaa').indexOf('你好', 'ucs2'), -1); |
| 241 | + |
236 | 242 | { |
237 | 243 | // Find substrings in Utf8. |
238 | 244 | const lengths = [1, 3, 15]; // Single char, simple and complex. |
|