The key or value object could be reclaimed by GC; fix #4164

The GC may occur between `sg_shift` and `mrb_assoc_new`, in which
case `key` and `value` could be freed even tough they are still alive.

The issue is found and fixed by https://hackerone.com/hexodus
This commit is contained in:
Yukihiro "Matz" Matsumoto
2018-11-16 01:04:57 +09:00
parent 0a022f7b8d
commit 180b73fec4
+2
View File
@@ -1057,6 +1057,8 @@ mrb_hash_shift(mrb_state *mrb, mrb_value hash)
mrb_value del_key, del_val;
sg_shift(mrb, sg, &del_key, &del_val);
mrb_gc_protect(mrb, del_key);
mrb_gc_protect(mrb, del_val);
return mrb_assoc_new(mrb, del_key, del_val);
}