Commit b7602e59

b7602e5924b5Merged

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

Description

runtime,codegen: Kernel#Integer(s, base) routes through strict base parser

Integer("ff", 16) used to ignore the base arg and call the plain sp_str_to_i_strict (decimal-only), raising "invalid value for Integer(): "ff"" for valid hex input. The exception was also uncatchable because the dispatch flowed before setjmp setup.

Add sp_str_to_i_strict_base accepting bases 2..36 with MRI prefix recognition (0x/0b/0o when base matches). Codegen routes the 2-arg form to the new helper. Both ok and error paths now go through the standard exception mechanism (catchable via begin/rescue).

Refs #887

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

Details

Commit
b7602e5924b5
Parent
f7416c07b180

lib/sp_runtime.hmodifiedView file

@@ -335,6 +335,51 @@
335335 return (mrb_int)v;
336336 }
337337
338+/* `Integer(s, base)` with explicit base. Bases 2..36, MRI-compatible
339+ prefix recognition (0x / 0b / 0o when the base matches). Raises
340+ ArgumentError on invalid input or unsupported base. Issue #887. */
341+static mrb_int sp_str_to_i_strict_base(const char *s, mrb_int base) {
342+ if (!s) sp_raise_cls("ArgumentError", "invalid value for Integer(): nil");
343+ if (base < 2 || base > 36) sp_raise_cls("ArgumentError", sp_sprintf("invalid radix %lld", (long long)base));
344+ const char *p = s;
345+ while (isspace((unsigned char)*p)) p++;
346+ int neg = 0;
347+ if (*p == '+') p++;
348+ else if (*p == '-') { neg = 1; p++; }
349+ if (*p == '0' && p[1] != 0) {
350+ if ((base == 16) && (p[1] == 'x' || p[1] == 'X')) p += 2;
351+ else if ((base == 2) && (p[1] == 'b' || p[1] == 'B')) p += 2;
352+ else if ((base == 8) && (p[1] == 'o' || p[1] == 'O')) p += 2;
353+ }
354+ if (*p == '\0') sp_raise_cls("ArgumentError", sp_sprintf("invalid value for Integer(): \"%s\"", s));
355+ mrb_int v = 0;
356+ int any = 0;
357+ while (*p) {
358+ int d = -1;
359+ if (*p >= '0' && *p <= '9') d = *p - '0';
360+ else if (*p >= 'a' && *p <= 'z') d = *p - 'a' + 10;
361+ else if (*p >= 'A' && *p <= 'Z') d = *p - 'A' + 10;
362+ if (d < 0 || d >= (int)base) {
363+ if (*p == '_' && any) {
364+ int n = -1;
365+ char c = p[1];
366+ if (c >= '0' && c <= '9') n = c - '0';
367+ else if (c >= 'a' && c <= 'z') n = c - 'a' + 10;
368+ else if (c >= 'A' && c <= 'Z') n = c - 'A' + 10;
369+ if (n >= 0 && n < (int)base) { p++; continue; }
370+ }
371+ break;
372+ }
373+ v = v * base + d;
374+ any = 1;
375+ p++;
376+ }
377+ if (!any) sp_raise_cls("ArgumentError", sp_sprintf("invalid value for Integer(): \"%s\"", s));
378+ while (isspace((unsigned char)*p)) p++;
379+ if (*p != '\0') sp_raise_cls("ArgumentError", sp_sprintf("invalid value for Integer(): \"%s\"", s));
380+ return neg ? -v : v;
381+}
382+
338383 /* Kernel#Float() raises ArgumentError on unparseable input. strtod
339384 on its own would silently return 0.0 for "abc" or empty input;
340385 match MRI semantics by validating at-least-one-digit + no-trailing-