use local variable for retval to avoid clobbering #640
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.
@lavarou 's great research and findings:
While I was looking for some information about -Wclobber and learning about zend_try/zend_catch I found these two interesting resources:
php/php-src#5151 - this basically shows that PHP team is aware of issues caused by -Wclobber and basically turn these warnings off (I don’t know if it’s good or bad that PHP is ignoring compiler warnings though; maybe they know what they’re doing)
https://github.com/php/php-src/blob/master/main/streams/userspace.c#L335-L340 - this is an example in PHP source code how zend_call_method_if_exists is called. It is a nice little trick of using an automatic variable to store user function call result. Based on this I came up with this idea: https://github.com/newrelic/newrelic-php-agent/compare/compiler_error...lavarou/fix/php-call-try-catch?expand=1. It seems to resolve the issue - I haven’t tested it past agent’s unit tests (make agent-valgrind) for PHP 8.2. Using automatic/local variable has this advantage that memory allocation is delayed until it is certain retval is good for use - i.e. zend_call_method_if_exists succeeded. Clobbering (longjmp called from within zend_call_method_if_exists which will cause this line to jump here) may invalidate the value of retval pointer and at the time it is freed here it may point to invalid memory and cause segfault. I don’t know this for certain. It would be awesome if there was a way to unit tests this code somehow and emulate happy path as well as exception path.