summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-struct
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2019-06-17 12:21:48 +0900
committerGitHub <[email protected]>2019-06-17 12:21:48 +0900
commite514264b531aa5b9c302a7b31bcc569f299cc82f (patch)
treecff3804ed30c3954e11e42225eae40ea317bbe66 /mrbgems/mruby-struct
parent0fba50082642fa4a6e0468f4e41192de095258f4 (diff)
parent9e378b451fe63c9e5b2ec9e55bf028b5f884174a (diff)
downloadmruby-e514264b531aa5b9c302a7b31bcc569f299cc82f.tar.gz
mruby-e514264b531aa5b9c302a7b31bcc569f299cc82f.zip
Merge pull request #4507 from shuujii/fix-index-in-error-message-of-Struct-aref
Fix index in error message of `Struct#[]`
Diffstat (limited to 'mrbgems/mruby-struct')
-rw-r--r--mrbgems/mruby-struct/src/struct.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/mrbgems/mruby-struct/src/struct.c b/mrbgems/mruby-struct/src/struct.c
index 30ef8d110..44822a03e 100644
--- a/mrbgems/mruby-struct/src/struct.c
+++ b/mrbgems/mruby-struct/src/struct.c
@@ -384,16 +384,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 */