Commit 31cd5ec9

31cd5ec9e92dMerged

Sam Ruby <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified

Description

runtime: stop sp_sprintf truncating long interpolations at 4095 bytes

Every string interpolation ("...#{x}...") compiles to sp_sprintf (compile_interpolated -> sp_sprintf("fmt", ...)). sp_sprintf formatted into a fixed char _sp_tmp[4096] stack buffer and then clamped the result length to sizeof(buf)-1:

if (_sp_n >= (int)sizeof(_sp_tmp)) _sp_n = (int)sizeof(_sp_tmp)-1;

so any interpolation producing >= 4096 bytes was silently truncated to 4095 -- the head survived, everything past 4095 (including the tail) was dropped.

vsnprintf already returns the would-be length, so re-render into a correctly-sized heap buffer (via va_copy) for the overflow case. The common < 4096 path keeps the stack-temp + memcpy fast path, so there's no extra formatting work for typical interpolations.

Surfaced in roundhouse view emit: a lowering that coalesces io << a; io << b; io << c append runs into a single io << "...#{}..." produces interpolations larger than 4096 bytes, which truncated rendered HTML pages mid-content. Direct String#<< concatenation was unaffected (it doesn't route through sp_sprintf); only interpolation hit the cap.

test/sprintf_long_interp.rb covers length + both endpoints of a 5002-byte interpolation; boundary-checked at 4095/4096/4097 and 100002.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
31cd5ec9e92d
Parent
776dd25921f7

lib/sp_runtime.hmodifiedView file

@@ -1333,7 +1333,7 @@
13331333 endpoints. Mirrors sp_IntArray_slice_range; issue #496. */
13341334 static const char*sp_str_sub_range_r(const char*s,mrb_int start,mrb_int end_,mrb_int excl){mrb_int cl=sp_str_length(s);if(end_<0)end_+=cl;if(start<0)start+=cl;mrb_int n=end_-start+(excl?0:1);if(n<0||start<0)n=0;return sp_str_sub_range_len(s,cl,start,n);}
13351335 static const char*sp_str_sub_range_len_r(const char*s,mrb_int cl,mrb_int start,mrb_int end_,mrb_int excl){if(end_<0)end_+=cl;if(start<0)start+=cl;mrb_int n=end_-start+(excl?0:1);if(n<0||start<0)n=0;return sp_str_sub_range_len(s,cl,start,n);}
1336-const char*sp_sprintf(const char*fmt,...){char _sp_tmp[4096];va_list ap;va_start(ap,fmt);int _sp_n=vsnprintf(_sp_tmp,sizeof(_sp_tmp),fmt,ap);va_end(ap);if(_sp_n<0)_sp_n=0;if(_sp_n>=(int)sizeof(_sp_tmp))_sp_n=(int)sizeof(_sp_tmp)-1;char*b=sp_str_alloc(_sp_n);memcpy(b,_sp_tmp,_sp_n);return b;}
1336+const char*sp_sprintf(const char*fmt,...){char _sp_tmp[4096];va_list ap;va_start(ap,fmt);int _sp_n=vsnprintf(_sp_tmp,sizeof(_sp_tmp),fmt,ap);va_end(ap);if(_sp_n<0)_sp_n=0;char*b=sp_str_alloc((size_t)_sp_n);if(_sp_n<(int)sizeof(_sp_tmp)){memcpy(b,_sp_tmp,(size_t)_sp_n);}else{/* result didn't fit the stack temp; re-render at full width (sp_str_alloc gives _sp_n bytes + NUL) so long string interpolations aren't truncated. re-arm the va_list rather than va_copy so the common fast path pays nothing */va_start(ap,fmt);vsnprintf(b,(size_t)_sp_n+1,fmt,ap);va_end(ap);}return b;}
13371337 /* Use a temp pointer for realloc so the original buffer is not leaked
13381338 on allocation failure. Match the perror+exit pattern used elsewhere
13391339 (see sp_IntArray_replace) instead of returning a partial result. */