From 635e2a8b337ffff6e64a27b722c4cd332752af1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Wed, 14 Jun 2017 10:46:50 +0200 Subject: [PATCH 1/2] util: ignore invalid format specifiers In util.format, if a percent sign without a known type is encountered, just print it instead of silently ignoring it and the next character. Fixes: https://github.com/nodejs/node/issues/13665 --- lib/util.js | 6 ++++++ test/parallel/test-util-format.js | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/lib/util.js b/lib/util.js index 921a684d50c2fb..4f6d727eaa7391 100644 --- a/lib/util.js +++ b/lib/util.js @@ -113,6 +113,12 @@ exports.format = function(f) { str += f.slice(lastPos, i); str += '%'; break; + default: // any other character is not a correct placeholder + if (lastPos < i) + str += f.slice(lastPos, i); + str += '%'; + lastPos = i = i + 1; + continue; } lastPos = i = i + 2; continue; diff --git a/test/parallel/test-util-format.js b/test/parallel/test-util-format.js index 93998c598a0e1f..1b4a40eaf9b6ec 100644 --- a/test/parallel/test-util-format.js +++ b/test/parallel/test-util-format.js @@ -126,6 +126,11 @@ assert.strictEqual(util.format('o: %j, a: %j', {}, []), 'o: {}, a: []'); assert.strictEqual(util.format('o: %j, a: %j', {}), 'o: {}, a: %j'); assert.strictEqual(util.format('o: %j, a: %j'), 'o: %j, a: %j'); +// Invalid format specifiers +assert.strictEqual(util.format('a% b', 'x'), 'a% b x'); +assert.strictEqual(util.format('percent: %d%, fraction: %d', 10, 0.1), + 'percent: 10%, fraction: 0.1'); + { const o = {}; o.o = o; From e8198c4213379815d23f4b0e725a04dd79d8bbbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Wed, 14 Jun 2017 11:46:25 +0200 Subject: [PATCH 2/2] fixup: add test with % at the end --- test/parallel/test-util-format.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/parallel/test-util-format.js b/test/parallel/test-util-format.js index 1b4a40eaf9b6ec..084e6f73b90ddf 100644 --- a/test/parallel/test-util-format.js +++ b/test/parallel/test-util-format.js @@ -130,6 +130,7 @@ assert.strictEqual(util.format('o: %j, a: %j'), 'o: %j, a: %j'); assert.strictEqual(util.format('a% b', 'x'), 'a% b x'); assert.strictEqual(util.format('percent: %d%, fraction: %d', 10, 0.1), 'percent: 10%, fraction: 0.1'); +assert.strictEqual(util.format('abc%', 1), 'abc% 1'); { const o = {};