Skip to content

Commit 00aac57

Browse files
targosjasnell
authored andcommitted
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: #13674 Fixes: #13665 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]>
1 parent 06ca205 commit 00aac57

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

lib/util.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ exports.format = function(f) {
113113
str += f.slice(lastPos, i);
114114
str += '%';
115115
break;
116+
default: // any other character is not a correct placeholder
117+
if (lastPos < i)
118+
str += f.slice(lastPos, i);
119+
str += '%';
120+
lastPos = i = i + 1;
121+
continue;
116122
}
117123
lastPos = i = i + 2;
118124
continue;

test/parallel/test-util-format.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ assert.strictEqual(util.format('o: %j, a: %j', {}, []), 'o: {}, a: []');
126126
assert.strictEqual(util.format('o: %j, a: %j', {}), 'o: {}, a: %j');
127127
assert.strictEqual(util.format('o: %j, a: %j'), 'o: %j, a: %j');
128128

129+
// Invalid format specifiers
130+
assert.strictEqual(util.format('a% b', 'x'), 'a% b x');
131+
assert.strictEqual(util.format('percent: %d%, fraction: %d', 10, 0.1),
132+
'percent: 10%, fraction: 0.1');
133+
assert.strictEqual(util.format('abc%', 1), 'abc% 1');
134+
129135
{
130136
const o = {};
131137
o.o = o;

0 commit comments

Comments
 (0)