-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathtest-buffer-copy-large.js
More file actions
44 lines (35 loc) · 1.65 KB
/
test-buffer-copy-large.js
File metadata and controls
44 lines (35 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
// Regression test for https://github.com/nodejs/node/issues/55422
// Buffer.copy and Buffer.concat silently produced incorrect results when
// indices involved were >= 2^32 due to 32-bit integer overflow in SlowCopy.
const common = require('../common');
const assert = require('assert');
// This test exercises the native SlowCopy path in node_buffer.cc.
// SlowCopy is invoked by _copyActual when the fast TypedArrayPrototypeSet
// path cannot be used (i.e. when sourceStart !== 0 or nb !== sourceLen).
// Cannot test on 32-bit machines since buffers that large cannot exist there.
common.skipIf32Bits();
const THRESHOLD = 2 ** 32; // 4 GiB
// Allocate a large target buffer (just over 4 GiB) to test that a targetStart
// value >= 2^32 is not silently truncated to its lower 32 bits.
let target;
try {
target = Buffer.alloc(THRESHOLD + 10, 0);
} catch (e) {
if (e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
/Array buffer allocation failed/.test(e.message)) {
common.skip('insufficient memory for large buffer allocation');
}
throw e;
}
const source = Buffer.alloc(10, 111);
// Copy only the first 5 bytes of source (nb=5, sourceLen=10 → nb !== sourceLen)
// so _copyActual falls through to the native _copy (SlowCopy) instead of
// using TypedArrayPrototypeSet. The targetStart is THRESHOLD (2^32), which
// previously overflowed to 0 when cast to uint32_t.
source.copy(target, THRESHOLD, 0, 5);
// The 5 copied bytes must appear at position THRESHOLD, not at position 0.
assert.strictEqual(target[0], 0);
assert.strictEqual(target[THRESHOLD], 111);
assert.strictEqual(target[THRESHOLD + 4], 111);
assert.strictEqual(target[THRESHOLD + 5], 0);