diff options
author | Michaƫl Zasso <targos@protonmail.com> | 2017-06-14 10:46:50 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2017-06-21 22:43:09 +0200 |
commit | c046a21321c18d2836e4248d3b960a52aacb415b (patch) | |
tree | 057cb5ed9b7799e12805e9d36ab195e8c1f7de23 | |
parent | e203e392d750343ff52154a99070962c92b2cee9 (diff) | |
download | node-new-c046a21321c18d2836e4248d3b960a52aacb415b.tar.gz |
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 <joyeec9h3@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
-rw-r--r-- | lib/util.js | 6 | ||||
-rw-r--r-- | test/parallel/test-util-format.js | 6 |
2 files changed, 12 insertions, 0 deletions
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; |