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 needle length misestimation for UCS2 · nodejs/node@12a9699

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
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,9 +827,9 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
827827
Local<String> needle = args[1].As<String>();
828828
const char* haystack = ts_obj_data;
829829
const size_t haystack_length = ts_obj_length;
830-
// Extended latin-1 characters are 2 bytes in Utf8.
830+
831831
const size_t needle_length =
832-
enc == BINARY ? needle->Length() : needle->Utf8Length();
832+
StringBytes::Size(args.GetIsolate(), needle, enc);
833833

834834

835835
if (needle_length == 0 || haystack_length == 0) {
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
@@ -233,6 +233,12 @@ var allCharsBufferUcs2 = new Buffer(allCharsString, 'ucs2');
233233
assert.equal(-1, allCharsBufferUtf8.indexOf('notfound'));
234234
assert.equal(-1, allCharsBufferUcs2.indexOf('notfound'));
235235

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

0 commit comments

Comments
0 (0)
You are visitor 01402503 — 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.