From: Vincent Pit Date: Wed, 26 Oct 2011 17:22:13 +0000 (+0200) Subject: Enforce saner checks on wizard SVs and MAGIC tokens X-Git-Tag: v0.47~9 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=8991ed7c69639359044bebf70ffaf8585b885252;p=perl%2Fmodules%2FVariable-Magic.git Enforce saner checks on wizard SVs and MAGIC tokens This commit adds a variety of helper functions that extract the vmg_wizard object from a SV or a MAGIC token. Version with (for direct interaction with the user) and without checks (for magic callbacks, where we know the MAGIC token is already valid) are provided. --- diff --git a/Magic.xs b/Magic.xs index 0d17eef..3c4cd33 100644 --- a/Magic.xs +++ b/Magic.xs @@ -591,6 +591,8 @@ STATIC const vmg_wizard *vmg_wizard_dup(pTHX_ const vmg_wizard *w, CLONE_PARAMS #endif /* VMG_THREADSAFE */ +#define vmg_wizard_id(W) PTR2IV(vmg_vtable_vtbl((W)->vtable)) + /* --- Wizard SV objects --------------------------------------------------- */ STATIC int vmg_wizard_sv_free(pTHX_ SV *sv, MAGIC *mg) { @@ -652,26 +654,11 @@ STATIC SV *vmg_wizard_sv_new(pTHX_ const vmg_wizard *w) { return wiz; } -STATIC const SV *vmg_wizard_sv_validate(pTHX_ const SV *wiz) { -#define vmg_wizard_sv_validate(W) vmg_wizard_sv_validate(aTHX_ (W)) - if (SvROK(wiz)) { - wiz = SvRV_const(wiz); #if VMG_THREADSAFE - if (SvTYPE(wiz) >= SVt_PVMG && SvMAGIC(wiz)) -#else - if (SvIOK(wiz)) -#endif - return wiz; - } - - croak(vmg_invalid_wiz); - /* Not reached */ - return NULL; -} -#if VMG_THREADSAFE +#define vmg_sv_has_wizard_type(S) (SvTYPE(S) >= SVt_PVMG) -STATIC const vmg_wizard *vmg_wizard_from_sv(const SV *wiz) { +STATIC const vmg_wizard *vmg_wizard_from_sv_nocheck(const SV *wiz) { MAGIC *mg; for (mg = SvMAGIC(wiz); mg; mg = mg->mg_moremagic) { @@ -682,35 +669,45 @@ STATIC const vmg_wizard *vmg_wizard_from_sv(const SV *wiz) { return NULL; } -#define vmg_wizard_from_sv(W) vmg_wizard_from_sv((const SV *) (W)) +#else /* VMG_THREADSAFE */ -#define vmg_wizard_sv_id(W) PTR2IV(vmg_vtable_vtbl(vmg_wizard_from_sv(W)->vtable)) +#define vmg_sv_has_wizard_type(S) SvIOK(S) -#else +#define vmg_wizard_from_sv_nocheck(W) INT2PTR(const vmg_wizard *, SvIVX(W)) -#define vmg_wizard_sv_id(W) SvIVX((const SV *) (W)) +#endif /* !VMG_THREADSAFE */ -#define vmg_wizard_from_sv(W) INT2PTR(const vmg_wizard *, vmg_wizard_sv_id(W)) +#define vmg_wizard_from_sv(W) (vmg_sv_has_wizard_type(W) ? vmg_wizard_from_sv_nocheck(W) : NULL) -#endif +STATIC const vmg_wizard *vmg_wizard_from_mg(const MAGIC *mg) { + if (mg->mg_type == PERL_MAGIC_ext && mg->mg_len == HEf_SVKEY) { + SV *sv = (SV *) mg->mg_ptr; + + if (vmg_sv_has_wizard_type(sv)) + return vmg_wizard_from_sv_nocheck(sv); + } + + return NULL; +} + +#define vmg_wizard_from_mg_nocheck(M) vmg_wizard_from_sv_nocheck((const SV *) (M)->mg_ptr) /* --- User-level functions implementation --------------------------------- */ -STATIC const MAGIC *vmg_find(const SV *sv, const SV *wiz) { - const MAGIC *mg, *moremagic; +STATIC const MAGIC *vmg_find(const SV *sv, const vmg_wizard *w) { + const MAGIC *mg; IV wid; if (SvTYPE(sv) < SVt_PVMG) return NULL; - wid = vmg_wizard_sv_id(wiz); - for (mg = SvMAGIC(sv); mg; mg = moremagic) { - moremagic = mg->mg_moremagic; - if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) { - IV zid = vmg_wizard_sv_id(mg->mg_ptr); - if (zid == wid) - return mg; - } + wid = vmg_wizard_id(w); + + for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { + const vmg_wizard *z = vmg_wizard_from_mg(mg); + + if (z && vmg_wizard_id(z) == wid) + return mg; } return NULL; @@ -752,9 +749,10 @@ STATIC SV *vmg_data_new(pTHX_ SV *ctor, SV *sv, SV **args, I32 items) { return nsv; } -STATIC SV *vmg_data_get(pTHX_ SV *sv, const SV *wiz) { +STATIC SV *vmg_data_get(pTHX_ SV *sv, const vmg_wizard *w) { #define vmg_data_get(S, W) vmg_data_get(aTHX_ (S), (W)) - const MAGIC *mg = vmg_find(sv, wiz); + const MAGIC *mg = vmg_find(sv, w); + return mg ? mg->mg_obj : NULL; } @@ -775,17 +773,15 @@ STATIC void vmg_uvar_del(SV *sv, MAGIC *prevmagic, MAGIC *mg, MAGIC *moremagic) } #endif /* VMG_UVAR */ -STATIC UV vmg_cast(pTHX_ SV *sv, const SV *wiz, SV **args, I32 items) { -#define vmg_cast(S, W, A, I) vmg_cast(aTHX_ (S), (W), (A), (I)) - const vmg_wizard *w; +STATIC UV vmg_cast(pTHX_ SV *sv, const vmg_wizard *w, const SV *wiz, SV **args, I32 items) { +#define vmg_cast(S, W, WIZ, A, I) vmg_cast(aTHX_ (S), (W), (WIZ), (A), (I)) MAGIC *mg; SV *data; U32 oldgmg; - if (vmg_find(sv, wiz)) + if (vmg_find(sv, w)) return 1; - w = vmg_wizard_from_sv(wiz); oldgmg = SvGMAGICAL(sv); data = (w->cb_data) ? vmg_data_new(w->cb_data, sv, args, items) : NULL; @@ -859,36 +855,39 @@ done: return 1; } -STATIC UV vmg_dispell(pTHX_ SV *sv, const SV *wiz) { +STATIC UV vmg_dispell(pTHX_ SV *sv, const vmg_wizard *w) { #define vmg_dispell(S, W) vmg_dispell(aTHX_ (S), (W)) #if VMG_UVAR U32 uvars = 0; #endif /* VMG_UVAR */ MAGIC *mg, *prevmagic, *moremagic = NULL; - IV wid = vmg_wizard_sv_id(wiz); + IV wid = vmg_wizard_id(w); if (SvTYPE(sv) < SVt_PVMG) return 0; for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) { + const vmg_wizard *z; + moremagic = mg->mg_moremagic; - if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) { + + z = vmg_wizard_from_mg(mg); + if (z) { + IV zid = vmg_wizard_id(z); + #if VMG_UVAR - const vmg_wizard *z = vmg_wizard_from_sv(mg->mg_ptr); -#endif /* VMG_UVAR */ - IV zid = vmg_wizard_sv_id(mg->mg_ptr); if (zid == wid) { -#if VMG_UVAR /* If the current has no uvar, short-circuit uvar deletion. */ uvars = z->uvar ? (uvars + 1) : 0; -#endif /* VMG_UVAR */ break; -#if VMG_UVAR } else if (z->uvar) { ++uvars; /* We can't break here since we need to find the ext magic to delete. */ -#endif /* VMG_UVAR */ } +#else /* VMG_UVAR */ + if (zid == wid) + break; +#endif /* !VMG_UVAR */ } } if (!mg) @@ -913,12 +912,11 @@ STATIC UV vmg_dispell(pTHX_ SV *sv, const SV *wiz) { /* mg was the first ext magic in the chain that had uvar */ for (mg = moremagic; mg; mg = mg->mg_moremagic) { - if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) { - const vmg_wizard *z = vmg_wizard_from_sv(mg->mg_ptr); - if (z->uvar) { - ++uvars; - break; - } + const vmg_wizard *z = vmg_wizard_from_mg(mg); + + if (z && z->uvar) { + ++uvars; + break; } } @@ -1072,17 +1070,19 @@ STATIC int vmg_cb_call(pTHX_ SV *cb, unsigned int flags, SV *sv, ...) { vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 3), (S), (A1), (A2), (A3)) STATIC int vmg_svt_get(pTHX_ SV *sv, MAGIC *mg) { - const vmg_wizard *w = vmg_wizard_from_sv(mg->mg_ptr); + const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg); + return vmg_cb_call1(w->cb_get, w->opinfo, sv, mg->mg_obj); } STATIC int vmg_svt_set(pTHX_ SV *sv, MAGIC *mg) { - const vmg_wizard *w = vmg_wizard_from_sv(mg->mg_ptr); + const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg); + return vmg_cb_call1(w->cb_set, w->opinfo, sv, mg->mg_obj); } STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) { - const vmg_wizard *w = vmg_wizard_from_sv(mg->mg_ptr); + const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg); unsigned int opinfo = w->opinfo; U32 len, ret; SV *svr; @@ -1136,7 +1136,8 @@ STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) { } STATIC int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) { - const vmg_wizard *w = vmg_wizard_from_sv(mg->mg_ptr); + const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg); + return vmg_cb_call1(w->cb_clear, w->opinfo, sv, mg->mg_obj); } @@ -1152,7 +1153,7 @@ STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) { if (PL_dirty) return 0; - w = vmg_wizard_from_sv(mg->mg_ptr); + w = vmg_wizard_from_mg_nocheck(mg); /* So that it survives the temp cleanup below */ SvREFCNT_inc_simple_void(sv); @@ -1202,7 +1203,7 @@ STATIC int vmg_svt_copy(pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *key, int keylen # endif ) { - const vmg_wizard *w = vmg_wizard_from_sv(mg->mg_ptr); + const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg); SV *keysv; int ret; @@ -1229,7 +1230,8 @@ STATIC int vmg_svt_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) { #if MGf_LOCAL STATIC int vmg_svt_local(pTHX_ SV *nsv, MAGIC *mg) { - const vmg_wizard *w = vmg_wizard_from_sv(mg->mg_ptr); + const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg); + return vmg_cb_call1(w->cb_local, w->opinfo, nsv, mg->mg_obj); } #endif /* MGf_LOCAL */ @@ -1268,8 +1270,8 @@ STATIC I32 vmg_svt_val(pTHX_ IV action, SV *sv) { default: continue; } - if (mg->mg_private != SIG_WIZ) continue; - w = vmg_wizard_from_sv(mg->mg_ptr); + w = vmg_wizard_from_mg(mg); + if (!w) continue; switch (w->uvar) { case 0: continue; @@ -1459,14 +1461,22 @@ OUTPUT: SV *cast(SV *sv, SV *wiz, ...) PROTOTYPE: \[$@%&*]$@ PREINIT: + const vmg_wizard *w = NULL; SV **args = NULL; + UV ret; I32 i = 0; CODE: if (items > 2) { i = items - 2; args = &ST(2); } - RETVAL = newSVuv(vmg_cast(SvRV(sv), vmg_wizard_sv_validate(wiz), args, i)); + if (SvROK(wiz)) { + wiz = SvRV_const(wiz); + w = vmg_wizard_from_sv(wiz); + } + if (!w) + croak(vmg_invalid_wiz); + RETVAL = newSVuv(vmg_cast(SvRV(sv), w, wiz, args, i)); OUTPUT: RETVAL @@ -1474,9 +1484,14 @@ void getdata(SV *sv, SV *wiz) PROTOTYPE: \[$@%&*]$ PREINIT: + const vmg_wizard *w = NULL; SV *data; PPCODE: - data = vmg_data_get(SvRV(sv), vmg_wizard_sv_validate(wiz)); + if (SvROK(wiz)) + w = vmg_wizard_from_sv(SvRV_const(wiz)); + if (!w) + croak(vmg_invalid_wiz); + data = vmg_data_get(SvRV(sv), w); if (!data) XSRETURN_EMPTY; ST(0) = data; @@ -1484,7 +1499,13 @@ PPCODE: SV *dispell(SV *sv, SV *wiz) PROTOTYPE: \[$@%&*]$ +PREINIT: + const vmg_wizard *w = NULL; CODE: - RETVAL = newSVuv(vmg_dispell(SvRV(sv), vmg_wizard_sv_validate(wiz))); + if (SvROK(wiz)) + w = vmg_wizard_from_sv(SvRV_const(wiz)); + if (!w) + croak(vmg_invalid_wiz); + RETVAL = newSVuv(vmg_dispell(SvRV(sv), w)); OUTPUT: RETVAL