[php-src] PR #17532: Drop misleading comments about undefined behavior
| From: | cmb69 | Date: | Mon, 20 Jan 2025 18:04:57 +0000 |
| Subject: | [php-src] PR #17532: Drop misleading comments about undefined behavior | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-30459@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/17532
Author: cmb69
There is no undefined behavior here. If
BitScan*() returns zero, the value written to
the first parameter is undefined, but we return a reasonable value.
---
This came up in #17527.
While having a closer look, I found that zend_ulong_ntz() and
zend_ulong_nlz() are documented to behave differently if the argument is zero. While
the latter states that for zero the result is undefined, the former states that for zero, LEN would
be returned:
https://github.com/php/php-src/blob/ce53dab2722611c2e87d680b9be43e187d82219f/Zend/zend_bitset.h#L47
While this is actually true on Windows, and for the fallback implementation, it is apparently not
the case when __builtin_ctzl() or __builtin_ctzll() are used. From https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html:
> Returns the number of trailing 0-bits in x, starting at the least significant bit position. If
> x is 0, the result is undefined.
So at least the comment should be fixed, and I would put in assert regarding the pre-condition, but
see https://github.com/php/php-src/pull/17506#discussion_r1922533709
(I'm not sure whether *all* callers use the function accordingly). Also, in that case the
Windows implementation would not need to check the return value.
Possible alternative: define the functions for num == 0.