Skip to content

Commit 971c7b2

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

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

src/library.js

+5
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

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ int main() {
3939
tm.tm_yday = 51;
4040
tm.tm_isdst = 0;
4141

42+
// Test %% escaping
43+
const char *fmt = "%%H=%H %%M=%m %%z=%z";
44+
strftime(s, sizeof(s), fmt, &tm);
45+
printf("%s -> %s\n", fmt, s);
46+
4247
size = strftime(s, 1000, "", &tm);
4348
test((size == 0) && (*s == '\0'), "strftime test #1", s);
4449

tests/core/test_strftime.out

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
%%H=%H %%M=%m %%z=%z -> %H=20 %M=02 %z=+0000
12
strftime test #1: 1
23
strftime test #2: 1
34
strftime test #3: 1

0 commit comments

Comments
 (0)