Skip to content

Commit f526386

Browse files
authored
Handle trailing % in strftime (#23045)
"In glibc a trailing % in strftime() acts like printf, ie it's a literal %". This is breaking some Python tests. I've applied the patch suggested here: https://www.openwall.com/lists/musl/2022/12/19/2
1 parent 81b7179 commit f526386

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

system/lib/libc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Some changes have been made to the version that was taken from upstream, includi
1212
* Switch to using the wasi `fd_write` syscall instead of `writev`.
1313
* Simplify stdout stream handling: do not support seeking, terminal handling, etc., as it just increases code size and Emscripten doesn't have those features anyhow.
1414
* Setting `_POSIX_REALTIME_SIGNALS` and `_POSIX_SPAWN` macros to -1, to exclude unsupported functions.
15+
* Handling trailing % in `strftime` and `wcsftime` format strings.
1516

1617
Copy log.c and log2.c from earlier version of musl which result in smaller
1718
binary size since they do not rely data tables in log_data.c and log2_data.c.

system/lib/libc/musl/src/time/strftime.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,13 @@ size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const st
226226
s[l] = 0;
227227
return l;
228228
}
229+
#ifdef __EMSCRIPTEN__
230+
// Handle trailing % by outputting a % rather than returning 0. Ideally
231+
// this 6 character change could be upstreamed into musl...
232+
if (*f != '%' || !f[1]) {
233+
#else
229234
if (*f != '%') {
235+
#endif
230236
s[l++] = *f;
231237
continue;
232238
}

test/other/test_strftime.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,5 +307,8 @@ int main() {
307307
size = strftime(s, sizeof(s), "%Ec", &tm);
308308
TEST(!cmp(s, "Mon Dec 17 00:00:00 2018"), "strftime test #36a", s);
309309

310+
size = strftime(s, sizeof(s), "trailing %", &tm);
311+
TEST((size == 10), "strftime test #37", s);
312+
TEST(!cmp(s, "trailing %"), "strftime test #37", s);
310313
return 0;
311314
}

0 commit comments

Comments
 (0)