amagic_call: don't invoke DB::sub for overloads called from DB

Perl decides on whether DB::sub is called for a sub called based on
the OPpENTERSUB_DB flag in the entersub op for that call.

At compilation time ck_subr sets if PL_curstash (the current
compilation stash) is not equal to PL_dbstash (%DB::), which is fine.

When calling an overload sub amagic_call() synthesizes an entersub OP
and sets OPpENTERSUB_DB based on the same condition, but PL_curstash
isn't set to the stash of the currently executing code, but is still
set to the last stash code was compiled in (as modified by scope
restoration).

This means the flag was (perhaps nearly) always set, so the debugger
would call &DB::sub even though the overload was occurring in package
DB.

Fix this by testing against CopSTASH(PL_curcop) instead, which other
runtime stash checks also do, eg. pp_caller, doeval_compile.

Fixes #24001
This commit is contained in:
Tony Cook 2025-12-17 14:42:36 +11:00
parent 997de1f574
commit b711f998fe
2 changed files with 1 additions and 2 deletions

2
gv.c
View File

@ -4171,7 +4171,7 @@ Perl_amagic_call(pTHX_ SV *left, SV *right, int method, int flags)
ENTER;
SAVEOP();
PL_op = (OP *) &myop;
if (PERLDB_SUB && PL_curstash != PL_debstash)
if (PERLDB_SUB && !CopSTASH_eq(PL_curcop, PL_debstash))
PL_op->op_private |= OPpENTERSUB_DB;
PUSHMARK(PL_stack_sp);

View File

@ -123,7 +123,6 @@ EXPECT
{
# GH 24001
local $::TODO = "&DB::sub shouldn't be called when called from DB";
local $ENV{PERL5DB} = 'sub DB::DB {}';
fresh_perl_is(<<'CODE', "OK\n",
package DB;