Skip to content

Commit 1bd4665

Browse files
committed
Fix escaping of % in strftime
Fixes: #16155
1 parent 95784cf commit 1bd4665

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

src/library.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,11 +1024,16 @@ LibraryManager.library = {
10241024
return '%';
10251025
}
10261026
};
1027+
1028+
// Replace %% with a pair of NULLs (which cannot occur in a C string), then
1029+
// re-inject them after processing.
1030+
pattern = pattern.replace(/%%/g, '\0\0')
10271031
for (var rule in EXPANSION_RULES_2) {
10281032
if (pattern.includes(rule)) {
10291033
pattern = pattern.replace(new RegExp(rule, 'g'), EXPANSION_RULES_2[rule](date));
10301034
}
10311035
}
1036+
pattern = pattern.replace(/\0\0/g, '%')
10321037

10331038
var bytes = intArrayFromString(pattern, false);
10341039
if (bytes.length > maxsize) {

tests/core/test_strftime.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,10 @@ int main() {
252252
size = strftime(s, 10, "%I %M %p", &tm);
253253
test(!cmp(s, "12 01 PM"), "strftime test #35", s);
254254

255+
// Test %% escaping
256+
const char *fmt = "%%H=%H %%M=%m %%z=%z";
257+
strftime(s, sizeof(s), fmt, &tm);
258+
printf("%s -> %s\n", fmt, s);
259+
255260
return 0;
256261
}

tests/core/test_strftime.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@ strftime test #32: 1
6060
strftime test #33: 1
6161
strftime test #34: 1
6262
strftime test #35: 1
63+
%%H=%H %%M=%m %%z=%z -> %H=12 %M=12 %z=-0800

0 commit comments

Comments
 (0)