Commit 4406c16d

4406c16d87b9Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified

Description

fix(runtime): str_to_i overflow + empty IntArray min/max guard

#743: sp_str_to_i_cruby's v = v * 10 + digit accumulator hit signed integer overflow on long digit strings (undefined behavior in C; the issue example produced 7886392056514347007 for a 29-digit input). Now each multiply + add goes through _builtin*_overflow; a detected overflow saturates the result at INT64_MAX (positive) or INT64_MIN (negative). CRuby returns a bignum here; spinel's int model is int64-only, so saturate as the closest representable answer.

#745: sp_IntArray_min / _max read a->data[a->start] before checking the array length. On an empty array (e.g. from [1].first(0)) the read is uninitialized memory -- random value under -O0, address- sanitized crash, or silent zero on the heap allocator's first behavior. Add an if (!a || a->len <= 0) return 0; guard mirroring sp_FloatArray_min's existing pattern.

Tests: test/str_to_i_overflow.rb - 6 cases including INT64_MAX boundary and a one-past saturation. test/empty_int_array_min_max.rb - empty .first(0) slice plus a non-empty sanity case.

471 tests pass; optcarrot checksum 59662.

Closes #743 Closes #745

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
4406c16d87b9
Parent
00b1763d3270

lib/sp_runtime.hmodifiedView file

@@ -236,9 +236,19 @@
236236 else if (*p == '-') { neg = 1; p++; }
237237 mrb_int v = 0;
238238 int any = 0;
239+ int saturated = 0;
239240 while (*p) {
240241 if (*p >= '0' && *p <= '9') {
241- v = v * 10 + (*p - '0');
242+ /* Issue #743: signed-overflow on `v * 10 + digit` is undefined
243+ behavior. Detect via __builtin_*_overflow and clamp to
244+ MRB_INT_MAX (CRuby returns a bignum here; spinel's int model
245+ is int64-only, so saturate as the next-best approximation). */
246+ mrb_int t;
247+ if (__builtin_mul_overflow(v, 10, &t) ||
248+ __builtin_add_overflow(t, (mrb_int)(*p - '0'), &v)) {
249+ v = INT64_MAX;
250+ saturated = 1;
251+ }
242252 any = 1;
243253 p++;
244254 } else if (*p == '_' && any && p[1] >= '0' && p[1] <= '9') {
@@ -248,6 +258,7 @@
248258 }
249259 }
250260 if (!any) return 0;
261+ if (saturated) return neg ? INT64_MIN : INT64_MAX;
251262 return neg ? -v : v;
252263 }
253264
@@ -913,8 +924,11 @@
913924 (0 / 0.0 / NULL / sp_box_nil for poly). Guards rand()%0, which
914925 raises SIGFPE under -O0 and silently UBs under -O2+. Issue #536. */
915926 static mrb_int sp_IntArray_sample(sp_IntArray*a){if(a->len<=0)return 0;return a->data[a->start+(mrb_int)(rand()%a->len)];}
916-static mrb_int sp_IntArray_min(sp_IntArray*a){mrb_int m=a->data[a->start];for(mrb_int i=1;i<a->len;i++)if(a->data[a->start+i]<m)m=a->data[a->start+i];return m;}
917-static mrb_int sp_IntArray_max(sp_IntArray*a){mrb_int m=a->data[a->start];for(mrb_int i=1;i<a->len;i++)if(a->data[a->start+i]>m)m=a->data[a->start+i];return m;}
927+/* Issue #745: guard the initial read on empty arrays. CRuby's [].min /
928+ .max return nil; spinel's int-typed slot collapses nil to 0. Without
929+ the guard, the first read is uninitialized memory. */
930+static mrb_int sp_IntArray_min(sp_IntArray*a){if(!a||a->len<=0)return 0;mrb_int m=a->data[a->start];for(mrb_int i=1;i<a->len;i++)if(a->data[a->start+i]<m)m=a->data[a->start+i];return m;}
931+static mrb_int sp_IntArray_max(sp_IntArray*a){if(!a||a->len<=0)return 0;mrb_int m=a->data[a->start];for(mrb_int i=1;i<a->len;i++)if(a->data[a->start+i]>m)m=a->data[a->start+i];return m;}
918932 static mrb_int sp_IntArray_sum(sp_IntArray*a,mrb_int init){mrb_int s=init;for(mrb_int i=0;i<a->len;i++)s+=a->data[a->start+i];return s;}
919933 static mrb_bool sp_IntArray_include(sp_IntArray*a,mrb_int v){for(mrb_int i=0;i<a->len;i++)if(a->data[a->start+i]==v)return TRUE;return FALSE;}
920934 static mrb_int sp_IntArray_index(sp_IntArray*a,mrb_int v){for(mrb_int i=0;i<a->len;i++)if(a->data[a->start+i]==v)return i;return -1;}