Skip to content

Commit c3feabe

Browse files
committed
avoid '0 + ptr' to create rvalues in macros
Pointer arithmetic (including 0 + ptr) has undefined behavior on null pointers. That's why the (0 + x->field) trick to provide rvalue access to struct fields is not generally safe with pointers. Instead use no-op casts, which also produce rvalues. Should fix these ASan errors (both from GvGP): gv.c:2920:44: runtime error: applying zero offset to null pointer sv.c:3855:25: runtime error: applying zero offset to null pointer
1 parent c5ead5d commit c3feabe

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

gv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ the need to cast the result to the appropriate type.
7676
# define GvNAMELEN_get(gv) ({ assert(GvNAME_HEK(gv)); HEK_LEN(GvNAME_HEK(gv)); })
7777
# define GvNAMEUTF8(gv) ({ assert(GvNAME_HEK(gv)); HEK_UTF8(GvNAME_HEK(gv)); })
7878
#else
79-
# define GvGP(gv) (0+(gv)->sv_u.svu_gp)
79+
# define GvGP(gv) ((GP *)(gv)->sv_u.svu_gp)
8080
# define GvGP_set(gv,gp) ((gv)->sv_u.svu_gp = (gp))
8181
# define GvFLAGS(gv) (GvXPVGV(gv)->xpv_cur)
8282
# define GvSTASH(gv) (GvXPVGV(gv)->xnv_u.xgv_stash)

op.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ C<sib> is non-null. For a higher-level interface, see C<L</op_sibling_splice>>.
10961096
#define OP_IS_STAT(op) (OP_IS_FILETEST(op) || (op) == OP_LSTAT || (op) == OP_STAT)
10971097

10981098
#define OpHAS_SIBLING(o) (cBOOL((o)->op_moresib))
1099-
#define OpSIBLING(o) (0 + (o)->op_moresib ? (o)->op_sibparent : NULL)
1099+
#define OpSIBLING(o) ((o)->op_moresib ? (o)->op_sibparent : NULL)
11001100
#define OpMORESIB_set(o, sib) ((o)->op_moresib = 1, (o)->op_sibparent = (sib))
11011101
#define OpLASTSIB_set(o, parent) \
11021102
((o)->op_moresib = 0, (o)->op_sibparent = (parent))

sv.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,20 +1350,20 @@ object type. Exposed to perl code via Internals::SvREADONLY().
13501350
# define SvIVX(sv) (0 + ((XPVIV*) SvANY(sv))->xiv_iv)
13511351
# define SvUVX(sv) (0 + ((XPVUV*) SvANY(sv))->xuv_uv)
13521352
# define SvNVX(sv) (-0.0 + ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv)
1353-
# define SvRV(sv) (0 + (sv)->sv_u.svu_rv)
1354-
# define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
1353+
# define SvRV_const(sv) ((SV *)(sv)->sv_u.svu_rv)
1354+
# define SvRV(sv) SvRV_const(sv)
13551355
/* Don't test the core XS code yet. */
13561356
# if defined (PERL_CORE) && PERL_DEBUG_COW > 1
1357-
# define SvPVX(sv) (0 + (assert_(!SvREADONLY(sv)) (sv)->sv_u.svu_pv))
1357+
# define SvPVX(sv) (assert_(!SvREADONLY(sv)) (char *)(sv)->sv_u.svu_pv)
13581358
# else
13591359
# define SvPVX(sv) SvPVX_mutable(sv)
13601360
# endif
13611361
# define SvCUR(sv) (0 + ((XPV*) SvANY(sv))->xpv_cur)
13621362
# define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
13631363
# define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
13641364

1365-
# define SvMAGIC(sv) (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*) SvANY(sv))->xmg_u.xmg_magic))
1366-
# define SvSTASH(sv) (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*) SvANY(sv))->xmg_stash))
1365+
# define SvMAGIC(sv) (assert_(SvTYPE(sv) >= SVt_PVMG) (MAGIC *)((XPVMG *)SvANY(sv))->xmg_u.xmg_magic)
1366+
# define SvSTASH(sv) (assert_(SvTYPE(sv) >= SVt_PVMG) (HV *)((XPVMG *)SvANY(sv))->xmg_stash)
13671367
#else /* Below is not PERL_DEBUG_COW */
13681368
# ifdef PERL_CORE
13691369
# define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
@@ -1452,7 +1452,7 @@ object type. Exposed to perl code via Internals::SvREADONLY().
14521452
# define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
14531453
# define SvNVX(sv) ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv
14541454
# define SvRV(sv) ((sv)->sv_u.svu_rv)
1455-
# define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
1455+
# define SvRV_const(sv) ((SV *)(sv)->sv_u.svu_rv)
14561456
# define SvMAGIC(sv) ((XPVMG*) SvANY(sv))->xmg_u.xmg_magic
14571457
# define SvSTASH(sv) ((XPVMG*) SvANY(sv))->xmg_stash
14581458
# endif

0 commit comments

Comments
 (0)