Skip to content

Commit 4629b18

Browse files
joyeecheungtargos
authored andcommitted
test: fix test-buffer-tostring-range on allocation failure
If the test cannot allocate a buffer over 4GB, there is no point continue testing toString() on it. This also split the test case to a different file for clarity and reduce interference with other cases. PR-URL: #58416 Fixes: #56726 Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Stefan Stojanovic <stefan.stojanovic@janeasystems.com>
1 parent 4c445a8 commit 4629b18

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
// This tests that Buffer.prototype.toString() works with buffers over 4GB.
4+
const common = require('../common');
5+
6+
// Must not throw when start and end are within kMaxLength
7+
// Cannot test on 32bit machine as we are testing the case
8+
// when start and end are above the threshold
9+
common.skipIf32Bits();
10+
const threshold = 0xFFFFFFFF; // 2^32 - 1
11+
let largeBuffer;
12+
try {
13+
largeBuffer = Buffer.alloc(threshold + 20);
14+
} catch (e) {
15+
if (e.code === 'ERR_MEMORY_ALLOCATION_FAILED' || /Array buffer allocation failed/.test(e.message)) {
16+
common.skip('insufficient space for Buffer.alloc');
17+
}
18+
throw e;
19+
}
20+
largeBuffer.toString('utf8', threshold, threshold + 20);

test/parallel/test-buffer-tostring-range.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const common = require('../common');
3+
require('../common');
44
const assert = require('assert');
55

66
const rangeBuffer = Buffer.from('abc');
@@ -98,11 +98,3 @@ assert.throws(() => {
9898
name: 'TypeError',
9999
message: 'Unknown encoding: null'
100100
});
101-
102-
// Must not throw when start and end are within kMaxLength
103-
// Cannot test on 32bit machine as we are testing the case
104-
// when start and end are above the threshold
105-
common.skipIf32Bits();
106-
const threshold = 0xFFFFFFFF;
107-
const largeBuffer = Buffer.alloc(threshold + 20);
108-
largeBuffer.toString('utf8', threshold, threshold + 20);

0 commit comments

Comments
 (0)