Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 38a07a9

Browse files
tomgcoindutny
authored andcommitted
util: handle escaped forward slashes correctly
Fixes #6835
1 parent 5106cad commit 38a07a9

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/util.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,8 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
413413
} else {
414414
name = name.replace(/'/g, "\\'")
415415
.replace(/\\"/g, '"')
416-
.replace(/(^"|"$)/g, "'");
416+
.replace(/(^"|"$)/g, "'")
417+
.replace(/\\\\/g, '\\');
417418
name = ctx.stylize(name, 'string');
418419
}
419420
}

test/simple/test-util-inspect.js

+15
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ assert.doesNotThrow(function() {
110110
var x = { inspect: util.inspect };
111111
assert.ok(util.inspect(x).indexOf('inspect') != -1);
112112

113+
// util.inspect should not display the escaped value of a key.
114+
var w = {
115+
'\\': 1,
116+
'\\\\': 2,
117+
'\\\\\\': 3,
118+
'\\\\\\\\': 4,
119+
}
120+
121+
var y = ['a', 'b', 'c'];
122+
y['\\\\\\'] = 'd';
123+
124+
assert.ok(util.inspect(w),
125+
'{ \'\\\': 1, \'\\\\\': 2, \'\\\\\\\': 3, \'\\\\\\\\\': 4 }');
126+
assert.ok(util.inspect(y), '[ \'a\', \'b\', \'c\', \'\\\\\\\': \'d\' ]');
127+
113128
// util.inspect.styles and util.inspect.colors
114129
function test_color_style(style, input, implicit) {
115130
var color_name = util.inspect.styles[style];

0 commit comments

Comments
 (0)