Replace ZEND_ASSUME()
by ZEND_ASSERT()
in zend_hash_*_ptr
setters
#14466
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.
I had a case where I was intentionally storing a
NULL
pointer within a HashTable to mark an entry as “blocked”, without paying for the overhead of checking the entry type when reading the pointer, resulting in semantics that are similar to usingisset()
instead ofarray_key_exists()
in userland.This worked fine in unoptimized test builds, but due to the
ZEND_ASSUME()
in thezend_hash_find_ptr
functions, the optimized release builds turned the logic of:into
thus introducing a hard-to-debug and compiler-dependent crash when the entry exists, but the stored pointer is
NULL
.Change the
ZEND_ASSUME()
in the setters toZEND_ASSERT()
. This would have made my mistake immediately obvious in debug builds when storing the pointer. The getters still useZEND_ASSUME()
under the assumption that they are called much more often, reducing the impact on debug builds: Assuming the developer uses the_ptr
variants for both reading and writing the entries, the mistake will be reliably caught during writing, making the assert during reading unnecessary.For release builds the
ZEND_ASSERT()
will be equivalent toZEND_ASSUME()
, avoiding any performance impact for those.