Commit 7c38a7c8

7c38a7c82a6fMerged

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

Description

runtime: to_i / Integer() on overflowing input raise RangeError

CRuby promotes oversize integers to Bignum; spinel's int model is int64-only, so the previous silent saturation at INT64_MAX hid the precision loss and made `rescue RangeError` ineffective. Replace each saturate path (sp_str_to_i_cruby, sp_str_to_i_base, sp_str_to_i_strict, sp_str_to_i_strict_base) with sp_raise_cls "RangeError" so a user-side rescue catches it.

Fixes #842

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

Details

Commit
7c38a7c82a6f
Parent
c41cb81060bb

lib/sp_runtime.hmodifiedView file

@@ -236,18 +236,17 @@
236236 else if (*p == '-') { neg = 1; p++; }
237237 mrb_int v = 0;
238238 int any = 0;
239- int saturated = 0;
240239 while (*p) {
241240 if (*p >= '0' && *p <= '9') {
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). */
241+ /* Signed-overflow on `v * 10 + digit` is undefined behavior;
242+ detect via __builtin_*_overflow. CRuby promotes to Bignum
243+ on overflow but spinel's int model is int64-only — raise
244+ RangeError instead of silently saturating, so a user-side
245+ `rescue` can react. */
246246 mrb_int t;
247247 if (__builtin_mul_overflow(v, 10, &t) ||
248248 __builtin_add_overflow(t, (mrb_int)(*p - '0'), &v)) {
249- v = INT64_MAX;
250- saturated = 1;
249+ sp_raise_cls("RangeError", sp_sprintf("integer overflow parsing \"%s\"", s));
251250 }
252251 any = 1;
253252 p++;
@@ -258,7 +257,6 @@
258257 }
259258 }
260259 if (!any) return 0;
261- if (saturated) return neg ? INT64_MIN : INT64_MAX;
262260 return neg ? -v : v;
263261 }
264262
@@ -283,7 +281,6 @@
283281 }
284282 mrb_int v = 0;
285283 int any = 0;
286- int saturated = 0;
287284 while (*p) {
288285 int d = -1;
289286 if (*p >= '0' && *p <= '9') d = *p - '0';
@@ -304,14 +301,12 @@
304301 mrb_int t;
305302 if (__builtin_mul_overflow(v, base, &t) ||
306303 __builtin_add_overflow(t, (mrb_int)d, &v)) {
307- v = INT64_MAX;
308- saturated = 1;
304+ sp_raise_cls("RangeError", sp_sprintf("integer overflow parsing \"%s\"", s));
309305 }
310306 any = 1;
311307 p++;
312308 }
313309 if (!any) return 0;
314- if (saturated) return neg ? INT64_MIN : INT64_MAX;
315310 return neg ? -v : v;
316311 }
317312
@@ -328,8 +323,13 @@
328323 while (isspace((unsigned char)*p)) p++;
329324 if (*p == '\0') sp_raise_cls("ArgumentError", sp_sprintf("invalid value for Integer(): \"%s\"", s));
330325 char *endptr;
326+ errno = 0;
331327 long long v = strtoll(p, &endptr, 10);
332328 if (endptr == p) sp_raise_cls("ArgumentError", sp_sprintf("invalid value for Integer(): \"%s\"", s));
329+ /* strtoll signals overflow via errno=ERANGE and clamps to
330+ LLONG_MAX/MIN — raise rather than silently saturate so the
331+ caller can distinguish "fits in int64" from "too big". */
332+ if (errno == ERANGE) sp_raise_cls("RangeError", sp_sprintf("integer overflow parsing \"%s\"", s));
333333 while (isspace((unsigned char)*endptr)) endptr++;
334334 if (*endptr != '\0') sp_raise_cls("ArgumentError", sp_sprintf("invalid value for Integer(): \"%s\"", s));
335335 return (mrb_int)v;
@@ -370,7 +370,13 @@
370370 }
371371 break;
372372 }
373- v = v * base + d;
373+ {
374+ mrb_int t;
375+ if (__builtin_mul_overflow(v, base, &t) ||
376+ __builtin_add_overflow(t, (mrb_int)d, &v)) {
377+ sp_raise_cls("RangeError", sp_sprintf("integer overflow parsing \"%s\"", s));
378+ }
379+ }
374380 any = 1;
375381 p++;
376382 }