diff --git a/extlib/benz/number.c b/extlib/benz/number.c index 3f9c3aa0..90b47f7e 100644 --- a/extlib/benz/number.c +++ b/extlib/benz/number.c @@ -216,12 +216,19 @@ pic_number_string_to_number(pic_state *pic) : pic_float_value(num); } - flo = pic_read_cstr(pic, str); + pic_try { + flo = pic_read_cstr(pic, str); + } + pic_catch { + // swallow error + flo = pic_false_value(); + } + if (pic_int_p(flo) || pic_float_p(flo)) { return flo; } - pic_errorf(pic, "invalid string given: %s", str); + return pic_false_value(); } void diff --git a/extlib/benz/read.c b/extlib/benz/read.c index eeca5485..b4bd1f4e 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -283,6 +283,10 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c) if (idx >= ATOF_BUF_SIZE) read_error(pic, "number too large", pic_obj_value(pic_make_str(pic, (const char *)buf, ATOF_BUF_SIZE))); + + if (! isdelim(c)) + read_error(pic, "non-delimiter character given after number", pic_list1(pic, pic_char_value(c))); + buf[idx] = 0; flt = PIC_CSTRING_TO_DOUBLE(buf); diff --git a/t/issue/322.scm b/t/issue/322.scm new file mode 100644 index 00000000..59bba7cc --- /dev/null +++ b/t/issue/322.scm @@ -0,0 +1,26 @@ +(import (scheme base) + (picrin test)) + +(test-begin) + +(test #f (string->number "12e43r")) + +(test #f (string->number "12e+43r")) + +(test #f (string->number "12e+43e54")) + +(test #f (string->number "12e+")) + +(test #f (string->number "12e")) + +(test #f (string->number "+12e")) + +(test #f (string->number "-12e")) + +(test -12 (string->number "-12")) + +(test -12.0 (string->number "-12.0")) + +(test #f (string->number "-12.0e-5t")) + +(test-end)