GeezerVison is showing github.com — buffer: fix odd UTF-16LE indexOf start · nodejs/node@128a78b
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 +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1086,6 +1086,10 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) { |
1086 | 1086 | } else if (is_forward && offset >= search_end) { |
1087 | 1087 | return args.GetReturnValue().Set(-1); |
1088 | 1088 | } |
| 1089 | + if (enc == UCS2 && is_forward) { |
| 1090 | + offset += offset % sizeof(uint16_t); |
| 1091 | + if (offset >= search_end) return args.GetReturnValue().Set(-1); |
| 1092 | + } |
1089 | 1093 | CHECK_LT(offset, haystack_length); |
1090 | 1094 | if ((is_forward && needle_length + offset > search_end) || |
1091 | 1095 | needle_length > search_end) { |
|
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 |
|---|
@@ -308,6 +308,12 @@ assert.strictEqual(Buffer.from('aaaa').indexOf('你好', 'ucs2'), -1); |
308 | 308 | // Haystack has odd length, but the needle is UCS2. |
309 | 309 | assert.strictEqual(Buffer.from('aaaaa').indexOf('b', 'ucs2'), -1); |
310 | 310 |
|
| 311 | +{ |
| 312 | + const buf = Buffer.from('\u6881\u6882\u6881', 'utf16le'); |
| 313 | + assert.strictEqual(buf.indexOf('\u6881', 1, 'utf16le'), 4); |
| 314 | + assert.strictEqual(buf.indexOf('\u6881', -1, 'utf16le'), -1); |
| 315 | +} |
| 316 | + |
311 | 317 | { |
312 | 318 | // Find substrings in Utf8. |
313 | 319 | const lengths = [1, 3, 15]; // Single char, simple and complex. |
|