Repository navigation
Fix GH-25340: ReflectionEnumBackedCase can be used on a non-backed enum via a constructor trick - #25872
Open
shuhuNB515 wants to merge 1 commit into
Open
Fix GH-25340: ReflectionEnumBackedCase can be used on a non-backed enum via a constructor trick#25872shuhuNB515 wants to merge 1 commit into
shuhuNB515 wants to merge 1 commit into
Conversation
… enum via a constructor trick When a subclass of ReflectionEnumBackedCase invokes ReflectionClassConstant::__construct() directly, the backed-enum check in ReflectionEnumBackedCase::__construct() is bypassed. A subsequent call to getBackingValue() then hit a ZEND_ASSERT() in debug builds, and performed an out-of-bounds property read (via zend_enum_fetch_case_value()) in release builds, because a unit enum case object has no backing value slot. Replace the assertion with a runtime check that throws a ReflectionException, consistent with the error message thrown by ReflectionEnumBackedCase::__construct().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes GH-25340
Problem
When a subclass of
ReflectionEnumBackedCaseinvokesReflectionClassConstant::__construct()directly (bypassing the backed-enum check inReflectionEnumBackedCase::__construct()), a subsequent call togetBackingValue()explodes:ZEND_ASSERT(intern->ce->enum_backing_type != IS_UNDEF)fails.zend_enum_fetch_case_value()reads property slot 1 (OBJ_PROP_NUM(zobj, 1)) of a unit enum case object, which has no such slot — an out-of-bounds read.Fix
Replace the assertion in
ReflectionEnumBackedCase::getBackingValue()with a runtime check that throws aReflectionException("Enum case %s::%s is not a backed case"), consistent with the error message thrown byReflectionEnumBackedCase::__construct()and with the existingIS_UNDEFguard in_enum_case_string().Verification
PHP 8.7.0-dev).Enum case NotBacked::A is not a backed case.ext/reflection/tests/ReflectionEnumBackedCase_getBackingValue_003.phptcovers this.ext/reflectionsuite: 570 passed, 0 failed.Zend/tests/enumsuite: 149 passed, 0 failed.