Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
test: skip the test if the buffer allocation fails
Use the error message as another condition to skip the test when the
buffer allocation fails.

Refs: 795dd8eb7988ae38553e
Refs: e9c6004a2d580008082b
  • Loading branch information
lpinca committed Jun 17, 2025
commit b65339bb10d1ddc5aed3c10abee86e35fb7a8f0c
18 changes: 14 additions & 4 deletions test/pummel/test-buffer-large-size-buffer-alloc-unsafe-slow.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@ common.skipIf32Bits();
const assert = require('node:assert');
const size = 2 ** 31;

let largeBuffer;

// Test Buffer.allocUnsafe with size larger than integer range
try {
assert.throws(() => Buffer.allocUnsafeSlow(size).toString('utf8'), { code: 'ERR_STRING_TOO_LONG' });
largeBuffer = Buffer.allocUnsafeSlow(size);
} catch (e) {
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
throw e;
if (
e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
/Array buffer allocation failed/.test(e.message)
) {
common.skip('insufficient space for Buffer.allocUnsafeSlow');
}
common.skip('insufficient space for Buffer.allocUnsafeSlow');

throw e;
}

assert.throws(() => largeBuffer.toString('utf8'), {
code: 'ERR_STRING_TOO_LONG',
});
21 changes: 14 additions & 7 deletions test/pummel/test-buffer-large-size-buffer-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ const kStringMaxLength = require('buffer').constants.MAX_STRING_LENGTH;

const size = 2 ** 31;

// Test Buffer.write with size larger than integer range
let largeBuffer;

try {
const buf = Buffer.alloc(size);
assert.strictEqual(buf.write('a', 2, kStringMaxLength), 1);
assert.strictEqual(buf.write('a', 2, size), 1);
largeBuffer = Buffer.alloc(size);
} catch (e) {
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
throw e;
if (
e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
/Array buffer allocation failed/.test(e.message)
) {
common.skip('insufficient space for Buffer.alloc');
}
common.skip('insufficient space for Buffer.alloc');

throw e;
}

// Test Buffer.write with size larger than integer range
assert.strictEqual(largeBuffer.write('a', 2, kStringMaxLength), 1);
assert.strictEqual(largeBuffer.write('a', 2, size), 1);
20 changes: 14 additions & 6 deletions test/pummel/test-buffer-large-size-slowbuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ const { Buffer } = require('node:buffer');

const size = 2 ** 31;

let largeBuffer;

// Test slow Buffer with size larger than integer range
try {
assert.throws(() => Buffer.allocUnsafeSlow(size).toString('utf8'), {
code: 'ERR_STRING_TOO_LONG',
});
largeBuffer = Buffer.allocUnsafeSlow(size);
} catch (e) {
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
throw e;
if (
e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
/Array buffer allocation failed/.test(e.message)
) {
common.skip('insufficient space for slow Buffer allocation');
}
common.skip('insufficient space for slow Buffer allocation');

throw e;
}

assert.throws(() => largeBuffer.toString('utf8'), {
code: 'ERR_STRING_TOO_LONG',
});
19 changes: 13 additions & 6 deletions test/pummel/test-string-decoder-large-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@ const stringTooLongError = {
name: 'Error',
};

let largeBuffer;

try {
const buf = Buffer.allocUnsafe(size);
const decoder = new StringDecoder('utf8');
assert.throws(() => decoder.write(buf), stringTooLongError);
largeBuffer = Buffer.allocUnsafe(size);
} catch (e) {
if (e.code !== 'ERR_MEMORY_ALLOCATION_FAILED') {
throw e;
if (
e.code === 'ERR_MEMORY_ALLOCATION_FAILED' ||
/Array buffer allocation failed/.test(e.message)
) {
common.skip('insufficient space for Buffer.alloc');
Comment thread
lpinca marked this conversation as resolved.
Outdated
}
common.skip('insufficient space for Buffer.alloc');

throw e;
}

const decoder = new StringDecoder('utf8');
assert.throws(() => decoder.write(largeBuffer), stringTooLongError);
Loading