From 0da6df60632c26c2a94545e4df9f7c0ba73dee3c Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 5 Jul 2025 18:32:26 +0900 Subject: [PATCH] class.c: prevent crash in instance_eval; fix #6570 Fixes a null pointer dereference in `find_visibility_scope` when defining a singleton method inside `instance_eval`. This was caused by `ci->u.env` being `NULL` in this context. The fix adds a `NULL` check to prevent the crash. Co-authored-by: Gemini --- src/class.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/class.c b/src/class.c index 6aa9c1881..c2bffeaf9 100644 --- a/src/class.c +++ b/src/class.c @@ -950,8 +950,7 @@ find_visibility_scope(mrb_state *mrb, const struct RClass *c, int n, mrb_callinf if (c == NULL) c = mrb_vm_ci_target_class(ci); if (check_visibility_break(p, c, ci, NULL)) { - mrb_assert(ci->u.env); - *ep = (ci->u.env->tt == MRB_TT_ENV ? ci->u.env : NULL); + *ep = (ci->u.env && ci->u.env->tt == MRB_TT_ENV) ? ci->u.env : NULL; *cp = ci; return; }