Skip to content

Commit

Permalink
util: do not throw when inspecting detached ArrayBuffer
Browse files Browse the repository at this point in the history
PR-URL: #29318
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
  • Loading branch information
addaleax authored and BridgeAR committed Sep 3, 2019
1 parent 8da9e4d commit 2986170
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,12 @@ function formatSpecialArray(ctx, value, recurseTimes, maxLength, output, i) {
}

function formatArrayBuffer(ctx, value) {
const buffer = new Uint8Array(value);
let buffer;
try {
buffer = new Uint8Array(value);
} catch {
return [ctx.stylize('(detached)', 'special')];
}
if (hexSlice === undefined)
hexSlice = uncurryThis(require('buffer').Buffer.prototype.hexSlice);
let str = hexSlice(buffer, 0, Math.min(ctx.maxArrayLength, buffer.length))
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const util = require('util');
const vm = require('vm');
const { previewEntries } = internalBinding('util');
const { inspect } = util;
const { MessageChannel } = require('worker_threads');

assert.strictEqual(util.inspect(1), '1');
assert.strictEqual(util.inspect(false), 'false');
Expand Down Expand Up @@ -198,6 +199,15 @@ assert(!/Object/.test(
' y: 1337\n}');
}

{
const ab = new ArrayBuffer(42);
assert.strictEqual(ab.byteLength, 42);
new MessageChannel().port1.postMessage(ab, [ ab ]);
assert.strictEqual(ab.byteLength, 0);
assert.strictEqual(util.inspect(ab),
'ArrayBuffer { (detached), byteLength: 0 }');
}

// Now do the same checks but from a different context.
{
const showHidden = false;
Expand Down

0 comments on commit 2986170

Please sign in to comment.