Skip to content

Commit d7e5a09

Browse files
committed
AvFILL: Evaluate argument exactly once
Macros should not evaluate their arguments more than once due to the possibility of the argument being an expression with side effects. This commit creates an inlined helper function to do the main processing.
1 parent 425bcc1 commit d7e5a09

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

av.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ If all you need is to look up an array element, then prefer C<av_fetch>.
114114
=for apidoc_defn ARm|SSize_t|av_top_index |NN AV *av
115115
=cut
116116
*/
117-
#define AvFILL(av) ((SvRMAGICAL((const SV *) (av))) \
118-
? mg_size(MUTABLE_SV(av)) : AvFILLp(av))
117+
#define AvFILL(av) AvFILL_(MUTABLE_AV(av))
119118
#define av_top_index(av) AvFILL(av)
120119
#define av_tindex(av) av_top_index(av)
121120

embed.fnc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,7 @@ ARdp |SV ** |av_fetch |NN AV *av \
712712
CRdip |SV ** |av_fetch_simple|NN AV *av \
713713
|SSize_t key \
714714
|I32 lval
715+
CRip |SSize_t|AvFILL_ |NN AV *av
715716
Adp |void |av_fill |NN AV *av \
716717
|SSize_t fill
717718
Cop |IV * |av_iter_p |NN AV *av

embed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999

100100
/* Hide global symbols */
101101

102+
# define AvFILL_(a) Perl_AvFILL_(aTHX_ a)
102103
# define Gv_AMupdate(a,b) Perl_Gv_AMupdate(aTHX_ a,b)
103104
# define SvAMAGIC_off Perl_SvAMAGIC_off
104105
# define SvAMAGIC_on Perl_SvAMAGIC_on

inline.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ Perl_av_count(pTHX_ AV *av)
6161
return AvFILL(av) + 1;
6262
}
6363

64+
PERL_STATIC_INLINE SSize_t
65+
Perl_AvFILL_(pTHX_ AV *av)
66+
{
67+
PERL_ARGS_ASSERT_AVFILL_;
68+
69+
if (SvRMAGICAL((const SV *) (av))) {
70+
return mg_size((SV *) av);
71+
}
72+
73+
return AvFILLp(av);
74+
}
75+
6476
/* ------------------------------- av.c ------------------------------- */
6577

6678
/*

proto.h

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)