fix stack over-read in os/strftime when the result does not fit (#1938)

This commit is contained in:
nabeel-dev21 authored and GitHub committed 2026-11-07 12:46:26 -06:00
1 parent c4a803c8fb
commit f63f7ccdf3
2 files changed
+8 -2

No files matched your search

+2 -2
View File
@@ -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) {
+6
View File
@@ -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")