Skip to content

test: fix test-buffer-tostring-range on allocation failure #58416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions test/parallel/test-buffer-tostring-4gb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

// This tests that Buffer.prototype.toString() works with buffers over 4GB.
const common = require('../common');

// Must not throw when start and end are within kMaxLength
// Cannot test on 32bit machine as we are testing the case
// when start and end are above the threshold
common.skipIf32Bits();
const threshold = 0xFFFFFFFF; // 2^32 - 1
let largeBuffer;
try {
largeBuffer = Buffer.alloc(threshold + 20);
} catch (e) {
if (e.code === 'ERR_MEMORY_ALLOCATION_FAILED' || /Array buffer allocation failed/.test(e.message)) {
common.skip('insufficient space for Buffer.alloc');
}
throw e;
}
largeBuffer.toString('utf8', threshold, threshold + 20);
10 changes: 1 addition & 9 deletions test/parallel/test-buffer-tostring-range.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const common = require('../common');
require('../common');
const assert = require('assert');

const rangeBuffer = Buffer.from('abc');
Expand Down Expand Up @@ -98,11 +98,3 @@ assert.throws(() => {
name: 'TypeError',
message: 'Unknown encoding: null'
});

// Must not throw when start and end are within kMaxLength
// Cannot test on 32bit machine as we are testing the case
// when start and end are above the threshold
common.skipIf32Bits();
const threshold = 0xFFFFFFFF;
const largeBuffer = Buffer.alloc(threshold + 20);
largeBuffer.toString('utf8', threshold, threshold + 20);
Loading