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

@@ -629,11 +629,15 @@
629629
630630 /* GC-aware Time trampolines. The libspinel_rt format helpers write
631631 into a local buffer; we sp_str_dup_external the result so the GC
632- tracks the lifetime. 256 bytes covers every format CRuby builds in
633- (the longest is %c at ~25 bytes); custom formats that exceed it get
634- truncated. */
632+ tracks the lifetime. strftime returns 0 -- never overruns the buffer
633+ -- when the formatted result would exceed it, which we surface as "".
634+ The 4 KB buffer covers any realistic format (CRuby's built-ins are
635+ ~25 bytes; this leaves room for long literal text or wide fields). A
636+ pathological field width (`"%1000000000F"`, which CRuby rejects with
637+ ERANGE) does not fit and yields "" -- a graceful empty string rather
638+ than a crash. */
635639 static const char *sp_time_strftime(sp_Time t, const char *fmt) {
636- char buf[256];
640+ char buf[4096];
637641 size_t n = sp_time_strftime_to(t, fmt, buf, sizeof(buf));
638642 if (n == 0) return SPL("");
639643 return sp_str_dup_external(buf);