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@04a1fd7

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • node_buffer.cc
  • test/parallel
    • 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
+16-16Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
11581158
}
11591159

11601160
args.GetReturnValue().Set(result >= search_end ? -1
1161-
: static_cast<int>(result));
1161+
: static_cast<double>(result));
11621162
}
11631163

11641164
void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
@@ -1246,14 +1246,14 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) {
12461246
}
12471247

12481248
args.GetReturnValue().Set(result >= search_end ? -1
1249-
: static_cast<int>(result));
1249+
: static_cast<double>(result));
12501250
}
12511251

1252-
int32_t IndexOfNumberImpl(Local<Value> buffer_obj,
1253-
const uint32_t needle,
1254-
const int64_t offset_i64,
1255-
const int64_t end_i64,
1256-
const bool is_forward) {
1252+
double IndexOfNumberImpl(Local<Value> buffer_obj,
1253+
const uint32_t needle,
1254+
const int64_t offset_i64,
1255+
const int64_t end_i64,
1256+
const bool is_forward) {
12571257
ArrayBufferViewContents<uint8_t> buffer(buffer_obj);
12581258
const uint8_t* buffer_data = buffer.data();
12591259
const size_t buffer_length = buffer.length();
@@ -1276,7 +1276,7 @@ int32_t IndexOfNumberImpl(Local<Value> buffer_obj,
12761276
ptr = nbytes::stringsearch::MemrchrFill(buffer_data, needle, backward_end);
12771277
}
12781278
const uint8_t* ptr_uint8 = static_cast<const uint8_t*>(ptr);
1279-
return ptr != nullptr ? static_cast<int32_t>(ptr_uint8 - buffer_data) : -1;
1279+
return ptr != nullptr ? static_cast<double>(ptr_uint8 - buffer_data) : -1;
12801280
}
12811281

12821282
void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) {
@@ -1297,14 +1297,14 @@ void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) {
12971297
IndexOfNumberImpl(buffer_obj, needle, offset_i64, end_i64, is_forward));
12981298
}
12991299

1300-
int32_t FastIndexOfNumber(Local<Value>,
1301-
Local<Value> buffer_obj,
1302-
uint32_t needle,
1303-
int64_t offset_i64,
1304-
bool is_forward,
1305-
int64_t end_i64,
1306-
// NOLINTNEXTLINE(runtime/references)
1307-
FastApiCallbackOptions& options) {
1300+
double FastIndexOfNumber(Local<Value>,
1301+
Local<Value> buffer_obj,
1302+
uint32_t needle,
1303+
int64_t offset_i64,
1304+
bool is_forward,
1305+
int64_t end_i64,
1306+
// NOLINTNEXTLINE(runtime/references)
1307+
FastApiCallbackOptions& options) {
13081308
HandleScope scope(options.isolate);
13091309
return IndexOfNumberImpl(buffer_obj, needle, offset_i64, end_i64, is_forward);
13101310
}
Collapse file

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

Copy file name to clipboard
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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, match), match);
23+
assert.strictEqual(buffer.indexOf(Buffer.from([0x0a]), match), match);
24+
assert.strictEqual(buffer.indexOf('\n', match), match);

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.