diff options
| author | KOBAYASHI Shuji <[email protected]> | 2019-06-15 19:41:04 +0900 |
|---|---|---|
| committer | KOBAYASHI Shuji <[email protected]> | 2019-06-15 19:41:04 +0900 |
| commit | 9e378b451fe63c9e5b2ec9e55bf028b5f884174a (patch) | |
| tree | a14d3be4c5475fd059c22e30b12aab72e7ef451f /mrbgems/mruby-struct/src/struct.c | |
| parent | ab5ffb308b65a365d5f68662f30b223ccbde3b03 (diff) | |
| download | mruby-9e378b451fe63c9e5b2ec9e55bf028b5f884174a.tar.gz mruby-9e378b451fe63c9e5b2ec9e55bf028b5f884174a.zip | |
Fix index in error message of `Struct#[]`
Before this patch:
$ bin/mruby -e 'Struct.new(:a,:b).new[-3]'
#=> offset -1 too small for struct(size:2) (IndexError)
After this patch (same as Ruby):
$ bin/mruby -e 'Struct.new(:a,:b).new[-3]'
#=> offset -3 too small for struct(size:2) (IndexError)
Diffstat (limited to 'mrbgems/mruby-struct/src/struct.c')
| -rw-r--r-- | mrbgems/mruby-struct/src/struct.c | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/mrbgems/mruby-struct/src/struct.c b/mrbgems/mruby-struct/src/struct.c index 40914acc9..939033d74 100644 --- a/mrbgems/mruby-struct/src/struct.c +++ b/mrbgems/mruby-struct/src/struct.c @@ -385,16 +385,17 @@ struct_aref_sym(mrb_state *mrb, mrb_value obj, mrb_sym id) static mrb_value struct_aref_int(mrb_state *mrb, mrb_value s, mrb_int i) { - if (i < 0) i = RSTRUCT_LEN(s) + i; - if (i < 0) - mrb_raisef(mrb, E_INDEX_ERROR, - "offset %S too small for struct(size:%S)", - mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); - if (RSTRUCT_LEN(s) <= i) + mrb_int idx = i < 0 ? RSTRUCT_LEN(s) + i : i; + + if (idx < 0) + mrb_raisef(mrb, E_INDEX_ERROR, + "offset %S too small for struct(size:%S)", + mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); + if (RSTRUCT_LEN(s) <= idx) mrb_raisef(mrb, E_INDEX_ERROR, "offset %S too large for struct(size:%S)", mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); - return RSTRUCT_PTR(s)[i]; + return RSTRUCT_PTR(s)[idx]; } /* 15.2.18.4.2 */ |
