From f63f7ccdf34956118e7a1922e2fc9515340ea31a Mon Sep 17 00:00:00 2001 From: nabeel-dev21 Date: Sun, 8 Nov 2026 00:16:26 +0530 Subject: [PATCH] fix stack over-read in os/strftime when the result does not fit (#1938) --- src/core/os.c | 4 ++-- test/suite-os.janet | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/core/os.c b/src/core/os.c index 24ae5cb1..f68ec97b 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -1996,8 +1996,8 @@ JANET_CORE_FN(os_strftime, struct tm t_infos; struct tm *t_info = time_to_tm(argv, argc, 1, &t_infos); char buf[SIZETIMEFMT]; - (void)strftime(buf, sizeof(buf), fmt, t_info); - return janet_cstringv(buf); + size_t len = strftime(buf, sizeof(buf), fmt, t_info); + return janet_stringv((const uint8_t *)buf, (int32_t) len); } static int entry_getdst(Janet env_entry) { diff --git a/test/suite-os.janet b/test/suite-os.janet index 965dff1a..e7adcce2 100644 --- a/test/suite-os.janet +++ b/test/suite-os.janet @@ -67,6 +67,12 @@ (assert (= (try (os/strftime "%H:%M:%") ([err] err)) "invalid conversion specifier") "invalid conversion specifier 2") +# strftime leaves its buffer indeterminate when the result does not fit, +# so an oversized result must not be read back with strlen. +(assert (= (os/strftime (string/repeat "a" 249) 0) (string/repeat "a" 249)) + "strftime result that fills the buffer") +(assert (= (os/strftime (string/repeat "%Y" 200) 0) "") + "strftime result too long for the buffer") # 07db4c530 (os/setenv "TESTENV1" "v1")