Skip to content

Commit 2986170

Browse files
addaleaxBridgeAR
authored andcommitted
util: do not throw when inspecting detached ArrayBuffer
PR-URL: #29318 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Gus Caplan <[email protected]>
1 parent 8da9e4d commit 2986170

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/internal/util/inspect.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,12 @@ function formatSpecialArray(ctx, value, recurseTimes, maxLength, output, i) {
11641164
}
11651165

11661166
function formatArrayBuffer(ctx, value) {
1167-
const buffer = new Uint8Array(value);
1167+
let buffer;
1168+
try {
1169+
buffer = new Uint8Array(value);
1170+
} catch {
1171+
return [ctx.stylize('(detached)', 'special')];
1172+
}
11681173
if (hexSlice === undefined)
11691174
hexSlice = uncurryThis(require('buffer').Buffer.prototype.hexSlice);
11701175
let str = hexSlice(buffer, 0, Math.min(ctx.maxArrayLength, buffer.length))

test/parallel/test-util-inspect.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const util = require('util');
2828
const vm = require('vm');
2929
const { previewEntries } = internalBinding('util');
3030
const { inspect } = util;
31+
const { MessageChannel } = require('worker_threads');
3132

3233
assert.strictEqual(util.inspect(1), '1');
3334
assert.strictEqual(util.inspect(false), 'false');
@@ -198,6 +199,15 @@ assert(!/Object/.test(
198199
' y: 1337\n}');
199200
}
200201

202+
{
203+
const ab = new ArrayBuffer(42);
204+
assert.strictEqual(ab.byteLength, 42);
205+
new MessageChannel().port1.postMessage(ab, [ ab ]);
206+
assert.strictEqual(ab.byteLength, 0);
207+
assert.strictEqual(util.inspect(ab),
208+
'ArrayBuffer { (detached), byteLength: 0 }');
209+
}
210+
201211
// Now do the same checks but from a different context.
202212
{
203213
const showHidden = false;

0 commit comments

Comments
 (0)