GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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
Filter options
  • src
    • node_buffer.cc
  • test/parallel
    • test-buffer-indexof.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/node_buffer.cc‎

Copy file name to clipboardExpand all lines: src/node_buffer.cc
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,10 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
10861086
} else if (is_forward && offset >= search_end) {
10871087
return args.GetReturnValue().Set(-1);
10881088
}
1089+
if (enc == UCS2 && is_forward) {
1090+
offset += offset % sizeof(uint16_t);
1091+
if (offset >= search_end) return args.GetReturnValue().Set(-1);
1092+
}
10891093
CHECK_LT(offset, haystack_length);
10901094
if ((is_forward && needle_length + offset > search_end) ||
10911095
needle_length > search_end) {
Collapse file

‎test/parallel/test-buffer-indexof.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-buffer-indexof.js
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,12 @@ assert.strictEqual(Buffer.from('aaaa').indexOf('你好', 'ucs2'), -1);
308308
// Haystack has odd length, but the needle is UCS2.
309309
assert.strictEqual(Buffer.from('aaaaa').indexOf('b', 'ucs2'), -1);
310310

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+
311317
{
312318
// Find substrings in Utf8.
313319
const lengths = [1, 3, 15]; // Single char, simple and complex.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.