From f52638627348b3f94911893bd05425d3ac8a384e Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Wed, 4 Dec 2024 22:12:59 +0100 Subject: [PATCH] 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 --- system/lib/libc/README.md | 1 + system/lib/libc/musl/src/time/strftime.c | 6 ++++++ test/other/test_strftime.c | 3 +++ 3 files changed, 10 insertions(+) diff --git a/system/lib/libc/README.md b/system/lib/libc/README.md index 895524e6f309c..0094e8a96dfe7 100644 --- a/system/lib/libc/README.md +++ b/system/lib/libc/README.md @@ -12,6 +12,7 @@ Some changes have been made to the version that was taken from upstream, includi * Switch to using the wasi `fd_write` syscall instead of `writev`. * 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. * Setting `_POSIX_REALTIME_SIGNALS` and `_POSIX_SPAWN` macros to -1, to exclude unsupported functions. + * Handling trailing % in `strftime` and `wcsftime` format strings. Copy log.c and log2.c from earlier version of musl which result in smaller binary size since they do not rely data tables in log_data.c and log2_data.c. diff --git a/system/lib/libc/musl/src/time/strftime.c b/system/lib/libc/musl/src/time/strftime.c index c40246dbb0fa3..59d609ba92245 100644 --- a/system/lib/libc/musl/src/time/strftime.c +++ b/system/lib/libc/musl/src/time/strftime.c @@ -226,7 +226,13 @@ size_t __strftime_l(char *restrict s, size_t n, const char *restrict f, const st s[l] = 0; return l; } +#ifdef __EMSCRIPTEN__ + // Handle trailing % by outputting a % rather than returning 0. Ideally + // this 6 character change could be upstreamed into musl... + if (*f != '%' || !f[1]) { +#else if (*f != '%') { +#endif s[l++] = *f; continue; } diff --git a/test/other/test_strftime.c b/test/other/test_strftime.c index 9f14e177c9e9b..26a388ba54b87 100644 --- a/test/other/test_strftime.c +++ b/test/other/test_strftime.c @@ -307,5 +307,8 @@ int main() { size = strftime(s, sizeof(s), "%Ec", &tm); TEST(!cmp(s, "Mon Dec 17 00:00:00 2018"), "strftime test #36a", s); + size = strftime(s, sizeof(s), "trailing %", &tm); + TEST((size == 10), "strftime test #37", s); + TEST(!cmp(s, "trailing %"), "strftime test #37", s); return 0; }