Browse Source

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>
v6
Michaël Zasso 8 years ago
committed by James M Snell
parent
commit
00aac57df9
  1. 6
      lib/util.js
  2. 6
      test/parallel/test-util-format.js

6
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;

6
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;

Loading…
Cancel
Save