Commit 9ba2e7c2
9ba2e7c2363bMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
Raise on out-of-range narrowing of foreign 64-bit values to Integer
Follow-up to the mrb_int -> intptr_t change. On a 32-bit build a foreign
64-bit value (time_t / off_t, or an strtoll result) can exceed the Ruby
Integer width. The boundary casts -- (mrb_int)tv_sec in Time#to_i and
(mrb_int)v in sp_str_to_i_strict -- previously truncated silently,
producing a garbage clock or a wrong Integer. int-overflow=wrap does
NOT license that: wrap is about the user's own arithmetic, not about
silently corrupting a system value the program never computed.
Add sp_i64_to_int(int64_t): identity on 64-bit mrb_int (no-op), and on 32-bit it raises RangeError when the value is outside the mrb_int range rather than truncating. Wire it into Time#to_i; add the same guard to sp_str_to_i_strict. The escape hatches for code that needs large timestamps under a 32-bit build are Time#to_f (mrb_float stays double) and holding the Time object (tv_sec is int64 internally).
promote-mode bigint promotion of these boundary values (instead of RangeError) is a separate follow-up tied to the promote-mode machinery.
Verified: 64-bit unchanged (679 tests, optcarrot 59662, bootstrap fixpoint -- intptr==int64 makes it a no-op); -m32 build returns Time.at(3000000000.0).to_i as RangeError (was a silent truncation) while Time.at(100).to_i stays 100.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 9ba2e7c2363b
- Parent
- c00688401d3a
lib/sp_core.cmodifiedView file
if (errno == ERANGE) sp_raise_cls("RangeError", sp_sprintf("integer overflow parsing \"%s\"", s)); while (isspace((unsigned char)*endptr)) endptr++; if (*endptr != '\0') sp_raise_cls("ArgumentError", sp_sprintf("invalid value for Integer(): \"%s\"", s)); /* On 32-bit mrb_int, a value that fit int64 (strtoll) may not fit the Ruby Integer; raise rather than silently truncate. No-op on 64-bit. */#if INTPTR_MAX != INT64_MAX if (v < (long long)INTPTR_MIN || v > (long long)INTPTR_MAX) sp_raise_cls("RangeError", sp_sprintf("integer %lld out of range for 32-bit Integer", v));#endif return (mrb_int)v;}lib/sp_runtime.hmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…