Skip to content

Commit c046a21

Browse files
targosaddaleax
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 e203e39 commit c046a21

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

lib/util.js

+6
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ exports.format = function(f) {
112112
str += f.slice(lastPos, i);
113113
str += '%';
114114
break;
115+
default: // any other character is not a correct placeholder
116+
if (lastPos < i)
117+
str += f.slice(lastPos, i);
118+
str += '%';
119+
lastPos = i = i + 1;
120+
continue;
115121
}
116122
lastPos = i = i + 2;
117123
continue;

test/parallel/test-util-format.js

+6
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)