GeezerVison is showing github.com — buffer: fix negative index for large buffers · HoonDongKang/node@905372a
File treeExpand 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 +7-7Lines changed: 7 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1157,8 +1157,8 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) { | 1157 | 1157 | is_forward); | 1158 | 1158 | } | 1159 | 1159 |
| 1160 | | - args.GetReturnValue().Set(result >= search_end ? -1 | 1161 | | - : static_cast<int>(result)); | | 1160 | + args.GetReturnValue().Set( | | 1161 | + result >= search_end ? -1 : static_cast<int64_t>(result)); | 1162 | 1162 | } | 1163 | 1163 |
| 1164 | 1164 | void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) { | @@ -1245,11 +1245,11 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) { | 1245 | 1245 | is_forward); | 1246 | 1246 | } | 1247 | 1247 |
| 1248 | | - args.GetReturnValue().Set(result >= search_end ? -1 | 1249 | | - : static_cast<int>(result)); | | 1248 | + args.GetReturnValue().Set( | | 1249 | + result >= search_end ? -1 : static_cast<int64_t>(result)); | 1250 | 1250 | } | 1251 | 1251 |
| 1252 | | -int32_t IndexOfNumberImpl(Local<Value> buffer_obj, | | 1252 | +int64_t IndexOfNumberImpl(Local<Value> buffer_obj, | 1253 | 1253 | const uint32_t needle, | 1254 | 1254 | const int64_t offset_i64, | 1255 | 1255 | const int64_t end_i64, | @@ -1276,7 +1276,7 @@ int32_t IndexOfNumberImpl(Local<Value> buffer_obj, | 1276 | 1276 | ptr = nbytes::stringsearch::MemrchrFill(buffer_data, needle, backward_end); | 1277 | 1277 | } | 1278 | 1278 | 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<int64_t>(ptr_uint8 - buffer_data) : -1; | 1280 | 1280 | } | 1281 | 1281 |
| 1282 | 1282 | void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) { | @@ -1297,7 +1297,7 @@ void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) { | 1297 | 1297 | IndexOfNumberImpl(buffer_obj, needle, offset_i64, end_i64, is_forward)); | 1298 | 1298 | } | 1299 | 1299 |
| 1300 | | -int32_t FastIndexOfNumber(Local<Value>, | | 1300 | +int64_t FastIndexOfNumber(Local<Value>, | 1301 | 1301 | Local<Value> buffer_obj, | 1302 | 1302 | uint32_t needle, | 1303 | 1303 | int64_t offset_i64, | |
Collapse file Copy file name to clipboard +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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); |
|