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 negative index for large buffers · nodejs/node@db67deb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • node_buffer.cc
  • test/pummel
    • test-buffer-indexof-large.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
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,8 +1098,8 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
10981098
is_forward);
10991099
}
11001100

1101-
args.GetReturnValue().Set(result >= search_end ? -1
1102-
: static_cast<int>(result));
1101+
args.GetReturnValue().Set(
1102+
result >= search_end ? -1 : static_cast<int64_t>(result));
11031103
}
11041104

11051105
void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
@@ -1186,11 +1186,11 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
11861186
is_forward);
11871187
}
11881188

1189-
args.GetReturnValue().Set(result >= search_end ? -1
1190-
: static_cast<int>(result));
1189+
args.GetReturnValue().Set(
1190+
result >= search_end ? -1 : static_cast<int64_t>(result));
11911191
}
11921192

1193-
int32_t IndexOfNumberImpl(Local<Value> buffer_obj,
1193+
int64_t IndexOfNumberImpl(Local<Value> buffer_obj,
11941194
const uint32_t needle,
11951195
const int64_t offset_i64,
11961196
const int64_t end_i64,
@@ -1217,7 +1217,7 @@ int32_t IndexOfNumberImpl(Local<Value> buffer_obj,
12171217
ptr = nbytes::stringsearch::MemrchrFill(buffer_data, needle, backward_end);
12181218
}
12191219
const uint8_t* ptr_uint8 = static_cast<const uint8_t*>(ptr);
1220-
return ptr != nullptr ? static_cast<int32_t>(ptr_uint8 - buffer_data) : -1;
1220+
return ptr != nullptr ? static_cast<int64_t>(ptr_uint8 - buffer_data) : -1;
12211221
}
12221222

12231223
void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) {
@@ -1238,7 +1238,7 @@ void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) {
12381238
IndexOfNumberImpl(buffer_obj, needle, offset_i64, end_i64, is_forward));
12391239
}
12401240

1241-
int32_t FastIndexOfNumber(Local<Value>,
1241+
int64_t FastIndexOfNumber(Local<Value>,
12421242
Local<Value> buffer_obj,
12431243
uint32_t needle,
12441244
int64_t offset_i64,
Collapse file

‎test/pummel/test-buffer-indexof-large.js‎

Copy file name to clipboard
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
6+
common.skipIf32Bits();
7+
8+
const match = 2 ** 31 + 100;
9+
let buffer;
10+
try {
11+
buffer = Buffer.allocUnsafe(match + 1);
12+
} catch (error) {
13+
if (error.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
14+
/Array buffer allocation failed/.test(error.message)) {
15+
common.skip('insufficient space for Buffer.allocUnsafe');
16+
}
17+
throw error;
18+
}
19+
20+
buffer[match] = 0x0a;
21+
22+
assert.strictEqual(buffer.indexOf(0x0a, -1), match);
23+
assert.strictEqual(buffer.indexOf(Buffer.from([0x0a]), -1), match);
24+
assert.strictEqual(buffer.indexOf('\n', -1), match);
25+
assert.strictEqual(buffer.lastIndexOf(0x0a, -1), match);
26+
assert.strictEqual(buffer.lastIndexOf(Buffer.from([0x0a]), -1), match);
27+
assert.strictEqual(buffer.lastIndexOf('\n', -1), match);

0 commit comments

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