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 <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>
1 parent 06ca205 commit 00aac57

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

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)