diff --git a/include/mruby/value.h b/include/mruby/value.h index 846452ce6..b791b7676 100644 --- a/include/mruby/value.h +++ b/include/mruby/value.h @@ -100,7 +100,9 @@ struct mrb_state; # define MRB_ENDIAN_LOHI(a,b) b a #endif -MRB_API mrb_int mrb_int_read(const char *p, const char *e, char **endp); +MRB_API mrb_bool mrb_read_int(const char *p, const char *e, char **endp, mrb_int *np); +/* obsolete; do not use mrb_int_read() */ +MRB_API mrb_int mrb_int_read(const char*, const char*, char**); #ifndef MRB_NO_FLOAT MRB_API double mrb_float_read(const char*, char**); #ifdef MRB_USE_FLOAT32 diff --git a/mrbgems/mruby-compiler/core/parse.y b/mrbgems/mruby-compiler/core/parse.y index d4258750f..9f4765771 100644 --- a/mrbgems/mruby-compiler/core/parse.y +++ b/mrbgems/mruby-compiler/core/parse.y @@ -6328,8 +6328,8 @@ parser_yylex(parser_state *p) if (last_state == EXPR_FNAME) goto gvar; tokfix(p); { - mrb_int n = mrb_int_read(tok(p), NULL, NULL); - if (n > INT32_MAX) { + mrb_int n; + if (!mrb_read_int(tok(p), NULL, NULL, &n)) { yywarning(p, "capture group index too big; always nil"); return keyword_nil; } diff --git a/mrbgems/mruby-compiler/core/y.tab.c b/mrbgems/mruby-compiler/core/y.tab.c index c818f299c..7d0d9b82c 100644 --- a/mrbgems/mruby-compiler/core/y.tab.c +++ b/mrbgems/mruby-compiler/core/y.tab.c @@ -12524,8 +12524,8 @@ parser_yylex(parser_state *p) if (last_state == EXPR_FNAME) goto gvar; tokfix(p); { - mrb_int n = mrb_int_read(tok(p), NULL, NULL); - if (n > INT32_MAX) { + mrb_int n; + if (!mrb_read_int(tok(p), NULL, NULL, &n)) { yywarning(p, "capture group index too big; always nil"); return keyword_nil; } diff --git a/mrbgems/mruby-pack/src/pack.c b/mrbgems/mruby-pack/src/pack.c index de667362f..48a820208 100644 --- a/mrbgems/mruby-pack/src/pack.c +++ b/mrbgems/mruby-pack/src/pack.c @@ -1306,8 +1306,8 @@ alias: ch = tptr[tmpl->idx]; if (ISDIGIT(ch)) { char *e; - mrb_int n = mrb_int_read(tptr+tmpl->idx, tptr+tlen, &e); - if (e == NULL || n > INT_MAX) { + mrb_int n; + if (!mrb_read_int(tptr+tmpl->idx, tptr+tlen, &e, &n)) { mrb_raise(mrb, E_RUNTIME_ERROR, "too big template length"); } count = (int)n; diff --git a/mrbgems/mruby-sprintf/src/sprintf.c b/mrbgems/mruby-sprintf/src/sprintf.c index 2ceaffd5f..7104a185a 100644 --- a/mrbgems/mruby-sprintf/src/sprintf.c +++ b/mrbgems/mruby-sprintf/src/sprintf.c @@ -254,12 +254,14 @@ check_name_arg(mrb_state *mrb, int posarg, const char *name, size_t len) num = (int)mrb_as_int(mrb, tmp_v); \ } while (0) -static const char * +static const char* get_num(mrb_state *mrb, const char *p, const char *end, int *valp) { char *e; - mrb_int n = mrb_int_read(p, end, &e); - if (e == NULL || n > INT_MAX) return NULL; + mrb_int n; + if (!mrb_read_int(p, end, &e, &n)) { + return NULL; + } *valp = (int)n; return e; } diff --git a/src/readint.c b/src/readint.c index 5fae222c2..ad7c048e7 100644 --- a/src/readint.c +++ b/src/readint.c @@ -1,16 +1,15 @@ #include #include -#include -/* mrb_int_read(): read mrb_int from a string (base 10 only) */ +/* mrb_read_int(): read mrb_int from a string (base 10 only) */ /* const char *p - string to read */ /* const char *e - end of string */ /* char **endp - end of parsed integer */ - -/* if integer overflows, errno will be set to ERANGE */ -/* also endp will be set to NULL on overflow */ -MRB_API mrb_int -mrb_int_read(const char *p, const char *e, char **endp) +/* mrb_int *np - variable to save the result */ +/* returns TRUE if read succeeded */ +/* if integer overflows, returns FALSE */ +MRB_API mrb_bool +mrb_read_int(const char *p, const char *e, char **endp, mrb_int *np) { mrb_int n = 0; int ch; @@ -19,12 +18,11 @@ mrb_int_read(const char *p, const char *e, char **endp) ch = *p - '0'; if (mrb_int_mul_overflow(n, 10, &n) || mrb_int_add_overflow(n, ch, &n)) { - if (endp) *endp = NULL; - errno = ERANGE; - return MRB_INT_MAX; + return FALSE; } p++; } if (endp) *endp = (char*)p; - return n; + *np = n; + return TRUE; }