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..084e6f73b90ddf 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;