From 369f64a9c48dbbf8b2cbe731a1a3f4f552a40705 Mon Sep 17 00:00:00 2001 From: buji Date: Mon, 13 Nov 2017 19:44:16 +0800 Subject: [PATCH 1/2] util: escaping object keys in util.inspect() Fixes: https://github.com/nodejs/node/issues/16979 --- lib/util.js | 32 +----------------------------- test/parallel/test-util-inspect.js | 15 +++++++------- 2 files changed, 9 insertions(+), 38 deletions(-) diff --git a/lib/util.js b/lib/util.js index 6d041fdf425bb7..051f5f419fb1e7 100644 --- a/lib/util.js +++ b/lib/util.js @@ -81,9 +81,7 @@ var Debug; /* eslint-disable */ const strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c]/; -const keyEscapeSequencesRegExp = /[\x00-\x1f\x27]/; const strEscapeSequencesReplacer = /[\x00-\x1f\x27\x5c]/g; -const keyEscapeSequencesReplacer = /[\x00-\x1f\x27]/g; /* eslint-enable */ const keyStrRegExp = /^[a-zA-Z_][a-zA-Z_0-9]*$/; const colorRegExp = /\u001b\[\d\d?m/g; @@ -137,34 +135,6 @@ function strEscape(str) { return `'${result}'`; } -// Escape control characters and single quotes. -// Note: for performance reasons this is not combined with strEscape -function keyEscape(str) { - if (str.length < 5000 && !keyEscapeSequencesRegExp.test(str)) - return `'${str}'`; - if (str.length > 100) - return `'${str.replace(keyEscapeSequencesReplacer, escapeFn)}'`; - var result = ''; - var last = 0; - for (var i = 0; i < str.length; i++) { - const point = str.charCodeAt(i); - if (point === 39 || point < 32) { - if (last === i) { - result += meta[point]; - } else { - result += `${str.slice(last, i)}${meta[point]}`; - } - last = i + 1; - } - } - if (last === 0) { - result = str; - } else if (last !== i) { - result += str.slice(last); - } - return `'${result}'`; -} - function tryStringify(arg) { try { return JSON.stringify(arg); @@ -859,7 +829,7 @@ function formatProperty(ctx, value, recurseTimes, key, array) { } else if (keyStrRegExp.test(key)) { name = ctx.stylize(key, 'name'); } else { - name = ctx.stylize(keyEscape(key), 'string'); + name = ctx.stylize(strEscape(key), 'string'); } return `${name}: ${str}`; diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index c0f0c6b6444baf..0c56a9fa0e900d 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -570,22 +570,23 @@ assert.doesNotThrow(() => { // util.inspect should not display the escaped value of a key. { const w = { - '\\': 1, - '\\\\': 2, - '\\\\\\': 3, - '\\\\\\\\': 4, + '\\': 2, + '\\\\': 4, + '\\\\\\': 6, + '\\\\\\\\': 8, }; const y = ['a', 'b', 'c']; - y['\\\\\\'] = 'd'; + y['\\\\'] = 'd'; assert.strictEqual( util.inspect(w), - '{ \'\\\': 1, \'\\\\\': 2, \'\\\\\\\': 3, \'\\\\\\\\\': 4 }' + '{ \'\\\\\': 2, \'\\\\\\\\\': 4, \'\\\\\\\\\\\\\': 6, ' + + '\'\\\\\\\\\\\\\\\\\': 8 }' ); assert.strictEqual( util.inspect(y), - '[ \'a\', \'b\', \'c\', \'\\\\\\\': \'d\' ]' + '[ \'a\', \'b\', \'c\', \'\\\\\\\\\': \'d\' ]' ); } From a61e8dfbdb7c81bf94edf64deebc0d6fbe3b0d6c Mon Sep 17 00:00:00 2001 From: buji Date: Tue, 14 Nov 2017 18:47:44 +0800 Subject: [PATCH 2/2] =?UTF-8?q?test=EF=BC=9Aadd=20new=20test=20cases=20in?= =?UTF-8?q?=20test-util-inspect.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/parallel/test-util-inspect.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 0c56a9fa0e900d..896c74ad96747e 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -567,26 +567,31 @@ assert.doesNotThrow(() => { assert.strictEqual(util.inspect(x).includes('inspect'), true); } -// util.inspect should not display the escaped value of a key. +// util.inspect should display the escaped value of a key. { const w = { - '\\': 2, - '\\\\': 4, - '\\\\\\': 6, - '\\\\\\\\': 8, + '\\': 1, + '\\\\': 2, + '\\\\\\': 3, + '\\\\\\\\': 4, + '\n': 5, + '\r': 6 }; const y = ['a', 'b', 'c']; y['\\\\'] = 'd'; + y['\n'] = 'e'; + y['\r'] = 'f'; assert.strictEqual( util.inspect(w), - '{ \'\\\\\': 2, \'\\\\\\\\\': 4, \'\\\\\\\\\\\\\': 6, ' + - '\'\\\\\\\\\\\\\\\\\': 8 }' + '{ \'\\\\\': 1, \'\\\\\\\\\': 2, \'\\\\\\\\\\\\\': 3, ' + + '\'\\\\\\\\\\\\\\\\\': 4, \'\\n\': 5, \'\\r\': 6 }' ); assert.strictEqual( util.inspect(y), - '[ \'a\', \'b\', \'c\', \'\\\\\\\\\': \'d\' ]' + '[ \'a\', \'b\', \'c\', \'\\\\\\\\\': \'d\', ' + + '\'\\n\': \'e\', \'\\r\': \'f\' ]' ); }