Skip to content

Fix escaping of % in strftime #16184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,11 +1024,16 @@ LibraryManager.library = {
return '%';
}
};

// Replace %% with a pair of NULLs (which cannot occur in a C string), then
// re-inject them after processing.
pattern = pattern.replace(/%%/g, '\0\0')
for (var rule in EXPANSION_RULES_2) {
if (pattern.includes(rule)) {
pattern = pattern.replace(new RegExp(rule, 'g'), EXPANSION_RULES_2[rule](date));
}
}
pattern = pattern.replace(/\0\0/g, '%')

var bytes = intArrayFromString(pattern, false);
if (bytes.length > maxsize) {
Expand Down
5 changes: 5 additions & 0 deletions tests/core/test_strftime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ int main() {
tm.tm_yday = 51;
tm.tm_isdst = 0;

// Test %% escaping
const char *fmt = "%%H=%H %%M=%m %%z=%z";
strftime(s, sizeof(s), fmt, &tm);
printf("%s -> %s\n", fmt, s);

size = strftime(s, 1000, "", &tm);
test((size == 0) && (*s == '\0'), "strftime test #1", s);

Expand Down
1 change: 1 addition & 0 deletions tests/core/test_strftime.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
%%H=%H %%M=%m %%z=%z -> %H=20 %M=02 %z=+0000
strftime test #1: 1
strftime test #2: 1
strftime test #3: 1
Expand Down