Skip to content

Commit ccbbcc8

Browse files
committed
Use a static inline function instead of macro body for SvCANEXISTDELETE
The macro body needed two variables to use as temporaries. This meant lots of callsites with apparently-unused variables. Worse, an unsuspecting author might use this macro and accidentally corrupt existing variables called `mg` or `stash` in subtle hard-to-find bugs. Since we can now use static inline functions this is much neater moved into such a function, avoiding the risk of breaking those variables.
1 parent 6f6506a commit ccbbcc8

File tree

4 files changed

+15
-39
lines changed

4 files changed

+15
-39
lines changed

inline.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,20 @@ Perl_rpp_invoke_xs(pTHX_ CV *cv)
11771177
}
11781178

11791179

1180+
/* for SvCANEXISTDELETE() macro in pp.h */
1181+
PERL_STATIC_INLINE bool
1182+
Perl_sv_can_existdelete(pTHX_ SV *sv)
1183+
{
1184+
/* Anything without tie magic is fine */
1185+
MAGIC *mg;
1186+
if(!SvRMAGICAL(sv) || !(mg = mg_find(sv, PERL_MAGIC_tied)))
1187+
return true;
1188+
1189+
HV *stash = SvSTASH(SvRV(SvTIED_obj(sv, mg)));
1190+
return stash &&
1191+
gv_fetchmethod_autoload(stash, "EXISTS", TRUE) &&
1192+
gv_fetchmethod_autoload(stash, "DELETE", TRUE);
1193+
}
11801194

11811195

11821196
/* ----------------------------- regexp.h ----------------------------- */

pp.c

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5349,9 +5349,6 @@ PP(pp_aslice)
53495349
bool can_preserve = FALSE;
53505350

53515351
if (localizing) {
5352-
MAGIC *mg;
5353-
HV *stash;
5354-
53555352
can_preserve = SvCANEXISTDELETE(av);
53565353
}
53575354

@@ -5571,8 +5568,6 @@ S_do_delete_local(pTHX)
55715568
{
55725569
dSP;
55735570
const U8 gimme = GIMME_V;
5574-
const MAGIC *mg;
5575-
HV *stash;
55765571
const bool sliced = cBOOL(PL_op->op_private & OPpSLICE);
55775572
SV **unsliced_keysv = sliced ? NULL : sp--;
55785573
SV * const osv = POPs;
@@ -5874,9 +5869,6 @@ PP(pp_hslice)
58745869
bool can_preserve = FALSE;
58755870

58765871
if (localizing) {
5877-
MAGIC *mg;
5878-
HV *stash;
5879-
58805872
if (SvCANEXISTDELETE(hv))
58815873
can_preserve = TRUE;
58825874
}
@@ -6546,9 +6538,6 @@ PP_wrapped(pp_reverse, 0, 1)
65466538
if (SvMAGICAL(av)) {
65476539
SSize_t i, j;
65486540
SV *tmp = sv_newmortal();
6549-
/* For SvCANEXISTDELETE */
6550-
HV *stash;
6551-
const MAGIC *mg;
65526541
bool can_preserve = SvCANEXISTDELETE(av);
65536542

65546543
for (i = 0, j = av_top_index(av); i < j; ++i, --j) {
@@ -7490,8 +7479,6 @@ PP(pp_refassign)
74907479
case SVt_PVAV:
74917480
assert(key);
74927481
if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
7493-
MAGIC *mg;
7494-
HV *stash;
74957482
S_localise_aelem_lval(aTHX_ (AV *)left, key,
74967483
SvCANEXISTDELETE(left));
74977484
}
@@ -7500,8 +7487,6 @@ PP(pp_refassign)
75007487
case SVt_PVHV:
75017488
if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
75027489
assert(key);
7503-
MAGIC *mg;
7504-
HV *stash;
75057490
S_localise_helem_lval(aTHX_ (HV *)left, key,
75067491
SvCANEXISTDELETE(left));
75077492
}
@@ -7538,8 +7523,6 @@ PP_wrapped(pp_lvref,
75387523
mg->mg_flags |= MGf_PERSIST;
75397524
if (UNLIKELY(PL_op->op_private & OPpLVAL_INTRO)) {
75407525
if (elem) {
7541-
MAGIC *mg;
7542-
HV *stash;
75437526
assert(arg);
75447527
{
75457528
const bool can_preserve = SvCANEXISTDELETE(arg);
@@ -7568,8 +7551,6 @@ PP_wrapped(pp_lvrefslice, 0, 1)
75687551
bool can_preserve = FALSE;
75697552

75707553
if (UNLIKELY(localizing)) {
7571-
MAGIC *mg;
7572-
HV *stash;
75737554
SV **svp;
75747555

75757556
can_preserve = SvCANEXISTDELETE(av);

pp.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -701,14 +701,7 @@ True if this op will be the return value of an lvalue subroutine
701701
=cut */
702702
#define LVRET ((PL_op->op_private & OPpMAYBE_LVSUB) && is_lvalue_sub())
703703

704-
#define SvCANEXISTDELETE(sv) \
705-
(!SvRMAGICAL(sv) \
706-
|| !(mg = mg_find((const SV *) sv, PERL_MAGIC_tied)) \
707-
|| ( (stash = SvSTASH(SvRV(SvTIED_obj(MUTABLE_SV(sv), mg)))) \
708-
&& gv_fetchmethod_autoload(stash, "EXISTS", TRUE) \
709-
&& gv_fetchmethod_autoload(stash, "DELETE", TRUE) \
710-
) \
711-
)
704+
#define SvCANEXISTDELETE(sv) Perl_sv_can_existdelete(aTHX_ MUTABLE_SV(sv))
712705

713706
#ifdef PERL_CORE
714707

pp_hot.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4356,9 +4356,6 @@ PP(pp_helem)
43564356
}
43574357

43584358
if (localizing) {
4359-
MAGIC *mg;
4360-
HV *stash;
4361-
43624359
/* Try to preserve the existence of a tied hash
43634360
* element by using EXISTS and DELETE if possible.
43644361
* Fall back to FETCH and STORE otherwise. */
@@ -4604,9 +4601,6 @@ PP(pp_multideref)
46044601
SV** svp;
46054602

46064603
if (UNLIKELY(localizing)) {
4607-
MAGIC *mg;
4608-
HV *stash;
4609-
46104604
/* Try to preserve the existence of a tied array
46114605
* element by using EXISTS and DELETE if possible.
46124606
* Fall back to FETCH and STORE otherwise. */
@@ -4797,9 +4791,6 @@ PP(pp_multideref)
47974791
HE* he;
47984792

47994793
if (UNLIKELY(localizing)) {
4800-
MAGIC *mg;
4801-
HV *stash;
4802-
48034794
/* Try to preserve the existence of a tied hash
48044795
* element by using EXISTS and DELETE if possible.
48054796
* Fall back to FETCH and STORE otherwise. */
@@ -6652,9 +6643,6 @@ PP(pp_aelem)
66526643
}
66536644

66546645
if (UNLIKELY(localizing)) {
6655-
MAGIC *mg;
6656-
HV *stash;
6657-
66586646
/* Try to preserve the existence of a tied array
66596647
* element by using EXISTS and DELETE if possible.
66606648
* Fall back to FETCH and STORE otherwise. */

0 commit comments

Comments
 (0)