Fix issue #322 string->number should never signal error

This commit is contained in:
Doug Currie 2016-01-10 14:56:44 -05:00
parent 6db22b4b8c
commit ad0ef726fc
3 changed files with 39 additions and 2 deletions

View File

@ -216,12 +216,19 @@ pic_number_string_to_number(pic_state *pic)
: pic_float_value(num); : 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)) { if (pic_int_p(flo) || pic_float_p(flo)) {
return flo; return flo;
} }
pic_errorf(pic, "invalid string given: %s", str); return pic_false_value();
} }
void void

View File

@ -283,6 +283,10 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c)
if (idx >= ATOF_BUF_SIZE) if (idx >= ATOF_BUF_SIZE)
read_error(pic, "number too large", read_error(pic, "number too large",
pic_obj_value(pic_make_str(pic, (const char *)buf, ATOF_BUF_SIZE))); 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; buf[idx] = 0;
flt = PIC_CSTRING_TO_DOUBLE(buf); flt = PIC_CSTRING_TO_DOUBLE(buf);

26
t/issue/322.scm Normal file
View File

@ -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)