Commit 7c14aa10
7c14aa1022d8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Widen the strftime output buffer
sp_time_strftime formatted into a fixed 256-byte stack buffer. strftime
returns 0 (it never overruns the buffer) when the result would not fit,
which was surfaced as "" -- so any output over 255 bytes, e.g. a format
with lots of literal text or a wide field, was silently dropped to ""
(a "%Y-"*100 format returned "" instead of a 500-byte string).
Use a 4 KB buffer, which covers any realistic format while keeping the
helper a single direct strftime call. A pathological field width
("%1000000000F", which CRuby rejects with Errno::ERANGE) still does
not fit and yields "" -- a graceful empty string, never a crash or
buffer overflow.
Fixes #1100
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 7c14aa1022d8
- Parent
- 1704c621b8eb
lib/sp_runtime.hmodifiedView file
/* GC-aware Time trampolines. The libspinel_rt format helpers write into a local buffer; we sp_str_dup_external the result so the GC tracks the lifetime. 256 bytes covers every format CRuby builds in (the longest is %c at ~25 bytes); custom formats that exceed it get truncated. */ tracks the lifetime. strftime returns 0 -- never overruns the buffer -- when the formatted result would exceed it, which we surface as "". The 4 KB buffer covers any realistic format (CRuby's built-ins are ~25 bytes; this leaves room for long literal text or wide fields). A pathological field width (`"%1000000000F"`, which CRuby rejects with ERANGE) does not fit and yields "" -- a graceful empty string rather than a crash. */static const char *sp_time_strftime(sp_Time t, const char *fmt) { char buf[256]; char buf[4096]; size_t n = sp_time_strftime_to(t, fmt, buf, sizeof(buf)); if (n == 0) return SPL(""); return sp_str_dup_external(buf);test/time_strftime_long_format.rbaddedView file
Loading diff…
test/time_strftime_long_format.rb.expectedaddedView file
Loading diff…