From 49af1fca03173f415c5634dafdce3ee81cc01401 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 11 Jun 2021 12:26:58 +0900 Subject: readint.c: add new function `mrb_int_read`. Difference from `strtoul(3)`: * reads `mrb_int` based on configuration * specifies the end of the string * no sign interpretation * base 10 only --- mrbgems/mruby-pack/src/pack.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'mrbgems/mruby-pack') diff --git a/mrbgems/mruby-pack/src/pack.c b/mrbgems/mruby-pack/src/pack.c index fc0f3fa54..f0c5d8f1e 100644 --- a/mrbgems/mruby-pack/src/pack.c +++ b/mrbgems/mruby-pack/src/pack.c @@ -1234,17 +1234,16 @@ alias: /* read suffix [0-9*_!<>] */ while (tmpl->idx < tlen) { - ch = tptr[tmpl->idx++]; + ch = tptr[tmpl->idx]; if (ISDIGIT(ch)) { - count = ch - '0'; - while (tmpl->idx < tlen && ISDIGIT(tptr[tmpl->idx])) { - if (count+10 > INT_MAX/10) { - mrb_raise(mrb, E_RUNTIME_ERROR, "too big template length"); - } - ch = tptr[tmpl->idx++] - '0'; - count = count * 10 + ch; + char *e; + mrb_int n = mrb_int_read(tptr+tmpl->idx, tptr+tlen, &e); + if (e == NULL || n > INT_MAX) { + mrb_raise(mrb, E_RUNTIME_ERROR, "too big template length"); } - continue; /* special case */ + count = (int)n; + tmpl->idx += e - tptr; + continue; } else if (ch == '*') { count = -1; } else if (ch == '_' || ch == '!' || ch == '<' || ch == '>') { @@ -1258,10 +1257,11 @@ alias: } else if (ch == '>') { flags |= PACK_FLAG_GT; } - } else { - tmpl->idx--; + } + else { break; } + tmpl->idx++; } if ((flags & PACK_FLAG_LT) || (!(flags & PACK_FLAG_GT) && littleendian)) { -- cgit v1.2.3