VmClassMemberAccess() should only check access right.
The build was successful. Details

There is no need for this function to report errors. Function should have responsibility over a single part of the functionality provided. This change also corrects some error messages.
This commit is contained in:
Rafal Kupiec 2019-05-29 15:26:31 +02:00
parent d924d41a7a
commit a5fb8ff8be
Signed by: belliash
GPG Key ID: 4E829243E0CFE6B4
1 changed files with 13 additions and 19 deletions

View File

@ -1074,7 +1074,7 @@ static ph7_value *VmNewOperandStack(
/* Forward declaration */ /* Forward declaration */
static sxi32 VmRegisterSpecialFunction(ph7_vm *pVm); static sxi32 VmRegisterSpecialFunction(ph7_vm *pVm);
static int VmInstanceOf(ph7_class *pThis, ph7_class *pClass); static int VmInstanceOf(ph7_class *pThis, ph7_class *pClass);
static int VmClassMemberAccess(ph7_vm *pVm, ph7_class *pClass, const SyString *pAttrName, sxi32 iProtection, int bLog); static int VmClassMemberAccess(ph7_vm *pVm, ph7_class *pClass, sxi32 iProtection);
/* /*
* Prepare the Virtual Machine for byte-code execution. * Prepare the Virtual Machine for byte-code execution.
* This routine gets called by the PH7 engine after * This routine gets called by the PH7 engine after
@ -4193,7 +4193,7 @@ static sxi32 VmByteCodeExec(
&pClass->sName, &sName &pClass->sName, &sName
); );
} else { } else {
if(!VmClassMemberAccess(&(*pVm), pMeth->sFunc.pClass, &sName, pMeth->iProtection, FALSE)) { if(!VmClassMemberAccess(&(*pVm), pMeth->sFunc.pClass, pMeth->iProtection)) {
PH7_VmThrowError(&(*pVm), PH7_CTX_ERR, PH7_VmThrowError(&(*pVm), PH7_CTX_ERR,
"Access to the class method '%z->%z()' is forbidden", &pMeth->sFunc.pClass->sName, &sName); "Access to the class method '%z->%z()' is forbidden", &pMeth->sFunc.pClass->sName, &sName);
} }
@ -4244,7 +4244,7 @@ static sxi32 VmByteCodeExec(
if(pObjAttr) { if(pObjAttr) {
ph7_value *pValue = 0; /* cc warning */ ph7_value *pValue = 0; /* cc warning */
/* Check attribute access */ /* Check attribute access */
if(VmClassMemberAccess(&(*pVm), pClass, &pObjAttr->pAttr->sName, pObjAttr->pAttr->iProtection, FALSE)) { if(VmClassMemberAccess(&(*pVm), pClass, pObjAttr->pAttr->iProtection)) {
/* Load attribute */ /* Load attribute */
pValue = (ph7_value *)SySetAt(&pVm->aMemObj, pObjAttr->nIdx); pValue = (ph7_value *)SySetAt(&pVm->aMemObj, pObjAttr->nIdx);
if(pValue) { if(pValue) {
@ -4370,7 +4370,7 @@ static sxi32 VmByteCodeExec(
} else { } else {
ph7_value *pValue; ph7_value *pValue;
/* Check if the access to the attribute is allowed */ /* Check if the access to the attribute is allowed */
if(VmClassMemberAccess(&(*pVm), pClass, &pAttr->sName, pAttr->iProtection, TRUE)) { if(VmClassMemberAccess(&(*pVm), pClass, pAttr->iProtection)) {
/* Load the desired attribute */ /* Load the desired attribute */
pValue = (ph7_value *)SySetAt(&pVm->aMemObj, pAttr->nIdx); pValue = (ph7_value *)SySetAt(&pVm->aMemObj, pAttr->nIdx);
if(pValue) { if(pValue) {
@ -4380,6 +4380,9 @@ static sxi32 VmByteCodeExec(
pTos->nIdx = pAttr->nIdx; pTos->nIdx = pAttr->nIdx;
} }
} }
} else {
PH7_VmThrowError(&(*pVm), PH7_CTX_ERR, "Access to the class attribute '%z::$%z' is forbidden",
&pClass->sName, &pAttr->sName);
} }
} }
} }
@ -6216,9 +6219,7 @@ static int vm_builtin_get_class_methods(ph7_context *pCtx, int nArg, ph7_value *
static int VmClassMemberAccess( static int VmClassMemberAccess(
ph7_vm *pVm, /* Target VM */ ph7_vm *pVm, /* Target VM */
ph7_class *pClass, /* Target Class */ ph7_class *pClass, /* Target Class */
const SyString *pAttrName, /* Attribute name */ sxi32 iProtection /* Attribute protection level [i.e: public,protected or private] */
sxi32 iProtection, /* Attribute protection level [i.e: public,protected or private] */
int bLog /* TRUE to log forbidden access. */
) { ) {
if(iProtection != PH7_CLASS_PROT_PUBLIC) { if(iProtection != PH7_CLASS_PROT_PUBLIC) {
VmFrame *pFrame = pVm->pFrame; VmFrame *pFrame = pVm->pFrame;
@ -6229,30 +6230,23 @@ static int VmClassMemberAccess(
} }
pVmFunc = (ph7_vm_func *)pFrame->pUserData; pVmFunc = (ph7_vm_func *)pFrame->pUserData;
if(pVmFunc == 0 || (pVmFunc->iFlags & VM_FUNC_CLASS_METHOD) == 0) { if(pVmFunc == 0 || (pVmFunc->iFlags & VM_FUNC_CLASS_METHOD) == 0) {
goto dis; /* Access is forbidden */ return 0; /* Access is forbidden */
} }
if(iProtection == PH7_CLASS_PROT_PRIVATE) { if(iProtection == PH7_CLASS_PROT_PRIVATE) {
/* Must be the same instance */ /* Must be the same instance */
if((ph7_class *)pVmFunc->pUserData != pClass) { if((ph7_class *)pVmFunc->pUserData != pClass) {
goto dis; /* Access is forbidden */ return 0; /* Access is forbidden */
} }
} else { } else {
/* Protected */ /* Protected */
ph7_class *pBase = (ph7_class *)pVmFunc->pUserData; ph7_class *pBase = (ph7_class *)pVmFunc->pUserData;
/* Must be a derived class */ /* Must be a derived class */
if(!VmInstanceOf(pBase, pClass)) { if(!VmInstanceOf(pBase, pClass)) {
goto dis; /* Access is forbidden */ return 0; /* Access is forbidden */
} }
} }
} }
return 1; /* Access is granted */ return 1; /* Access is granted */
dis:
if(bLog) {
PH7_VmThrowError(&(*pVm), PH7_CTX_ERR,
"Access to the class attribute '%z->%z' is forbidden",
&pClass->sName, pAttrName);
}
return 0; /* Access is forbidden */
} }
/* /*
* array get_class_vars(string/object $class_name) * array get_class_vars(string/object $class_name)
@ -6295,7 +6289,7 @@ static int vm_builtin_get_class_vars(ph7_context *pCtx, int nArg, ph7_value **ap
while((pEntry = SyHashGetNextEntry(&pClass->hAttr)) != 0) { while((pEntry = SyHashGetNextEntry(&pClass->hAttr)) != 0) {
ph7_class_attr *pAttr = (ph7_class_attr *)pEntry->pUserData; ph7_class_attr *pAttr = (ph7_class_attr *)pEntry->pUserData;
/* Check if the access is allowed */ /* Check if the access is allowed */
if(VmClassMemberAccess(pCtx->pVm, pClass, &pAttr->sName, pAttr->iProtection, FALSE)) { if(VmClassMemberAccess(pCtx->pVm, pClass, pAttr->iProtection)) {
SyString *pAttrName = &pAttr->sName; SyString *pAttrName = &pAttr->sName;
ph7_value *pValue = 0; ph7_value *pValue = 0;
if(pAttr->iFlags & (PH7_CLASS_ATTR_CONSTANT | PH7_CLASS_ATTR_STATIC)) { if(pAttr->iFlags & (PH7_CLASS_ATTR_CONSTANT | PH7_CLASS_ATTR_STATIC)) {
@ -6370,7 +6364,7 @@ static int vm_builtin_get_object_vars(ph7_context *pCtx, int nArg, ph7_value **a
} }
pAttrName = &pVmAttr->pAttr->sName; pAttrName = &pVmAttr->pAttr->sName;
/* Check if the access is allowed */ /* Check if the access is allowed */
if(VmClassMemberAccess(pCtx->pVm, pThis->pClass, pAttrName, pVmAttr->pAttr->iProtection, FALSE)) { if(VmClassMemberAccess(pCtx->pVm, pThis->pClass, pVmAttr->pAttr->iProtection)) {
ph7_value *pValue = 0; ph7_value *pValue = 0;
/* Extract attribute */ /* Extract attribute */
pValue = PH7_ClassInstanceExtractAttrValue(pThis, pVmAttr); pValue = PH7_ClassInstanceExtractAttrValue(pThis, pVmAttr);