Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2986170

Browse filesBrowse files
addaleaxBridgeAR
authored andcommitted
util: do not throw when inspecting detached ArrayBuffer
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>
1 parent 8da9e4d commit 2986170
Copy full SHA for 2986170

File tree

Expand file treeCollapse file tree

2 files changed

+16
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+16
-1
lines changed
Open diff view settings
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+6-1Lines 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))
Collapse file

‎test/parallel/test-util-inspect.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-inspect.js
+10Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.