From 40d0d8fe0efa6a34b54ef90a93f16b17fe3f3c7b Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Sun, 31 May 2020 17:33:19 +0900 Subject: Fix `Fixnum` and `Float` comparison in `Hash` lookup ```console $ bin/mruby -e 'p({1 => 2}.key?(1.0))' true ``` ```console $ bin/mruby -e 'p({1 => 2}.key?(1.0))' false ``` --- src/hash.c | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/hash.c b/src/hash.c index 2eef4afaf..84ed00001 100644 --- a/src/hash.c +++ b/src/hash.c @@ -96,27 +96,13 @@ ht_hash_equal(mrb_state *mrb, htable *t, mrb_value a, mrb_value b) return mrb_symbol(a) == mrb_symbol(b); case MRB_TT_FIXNUM: - switch (mrb_type(b)) { - case MRB_TT_FIXNUM: - return mrb_fixnum(a) == mrb_fixnum(b); -#ifndef MRB_NO_FLOAT - case MRB_TT_FLOAT: - return (mrb_float)mrb_fixnum(a) == mrb_float(b); -#endif - default: - return FALSE; - } + if (!mrb_fixnum_p(b)) return FALSE; + return mrb_fixnum(a) == mrb_fixnum(b); #ifndef MRB_NO_FLOAT case MRB_TT_FLOAT: - switch (mrb_type(b)) { - case MRB_TT_FIXNUM: - return mrb_float(a) == (mrb_float)mrb_fixnum(b); - case MRB_TT_FLOAT: - return mrb_float(a) == mrb_float(b); - default: - return FALSE; - } + if (!mrb_float_p(b)) return FALSE; + return mrb_float(a) == mrb_float(b); #endif default: -- cgit v1.2.3