diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-06-11 12:26:58 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-06-11 15:14:17 +0900 |
| commit | 49af1fca03173f415c5634dafdce3ee81cc01401 (patch) | |
| tree | eb1a8ddfe0f2fcc5b10e6666322efd0964189b30 /mrbgems/mruby-pack | |
| parent | 14a69c6ffd08842a896066c3610c5fabcae3925f (diff) | |
| download | mruby-49af1fca03173f415c5634dafdce3ee81cc01401.tar.gz mruby-49af1fca03173f415c5634dafdce3ee81cc01401.zip | |
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
Diffstat (limited to 'mrbgems/mruby-pack')
| -rw-r--r-- | mrbgems/mruby-pack/src/pack.c | 22 |
1 files changed, 11 insertions, 11 deletions
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)) { |
