Fixed to prevent segmentation faults when iterating a message #25404
+34
−2
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.
Hello! I have found a solution to the issue in #22173, so I would like you to review it.
Problem
When attempting to iterate over a protobuf
Messageobject using PHP'sforeachconstruct, a segmentation fault occurs. This happens because theget_propertiesobject handler returnsNULL, which causes the Zend engine to dereference a null pointer when attempting iteration.Root Cause
The issue occurs in three object handlers:
Message_get_properties()inmessage.cAll three handlers were returning
NULLwith the comment "We don't have/offer a properties table." While this is intentional (protobuf messages are not meant to be iterable), returningNULLcauses the Zend engine to crash when PHP's foreach tries to use the result.From the stack trace in #22173:
The crash happens at
zend_hash.h:317wherezend_hash_num_elements(ht)is called with a NULL pointer.Solution
Instead of returning
NULL, we now ensure that a valid (but empty)HashTableis returned by callingzend_std_get_properties(). This is the standard Zend API function that:object->propertiesif it doesn't existHashTableChanges Made
Message_get_properties()to callzend_std_get_properties()and return a valid HashTabletestForeachOnMessageDoesNotSegfault()Testing
Manual Test
Before fix: Segmentation fault
After fix: No crash, zero iterations (expected behavior)
Unit Test
Added
testForeachOnMessageDoesNotSegfault()inGeneratedClassTest.php:TestMessagewith some fields setBuild and Test
Expected Behavior
After this fix:
Compatibility
zend_std_get_properties) available in all PHP versionsRelated Issues