Skip to content

Commit

Permalink
util: ignore invalid format specifiers
Browse files Browse the repository at this point in the history
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: nodejs#13665
  • Loading branch information
targos committed Jun 14, 2017
1 parent 448c4c6 commit 635e2a8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-util-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 635e2a8

Please sign in to comment.