From c046a21321c18d2836e4248d3b960a52aacb415b 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: 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. PR-URL: https://github.com/nodejs/node/pull/13674 Fixes: https://github.com/nodejs/node/issues/13665 Reviewed-By: Joyee Cheung Reviewed-By: Timothy Gu Reviewed-By: Colin Ihrig Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell Reviewed-By: Matteo Collina Reviewed-By: Daniel Bevenius --- lib/util.js | 6 ++++++ test/parallel/test-util-format.js | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/lib/util.js b/lib/util.js index d6d8b9de00..81e3a42f05 100644 --- a/lib/util.js +++ b/lib/util.js @@ -112,6 +112,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 93998c598a..084e6f73b9 100644 --- a/test/parallel/test-util-format.js +++ b/test/parallel/test-util-format.js @@ -126,6 +126,12 @@ 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'); +assert.strictEqual(util.format('abc%', 1), 'abc% 1'); + { const o = {}; o.o = o; -- cgit v1.2.1