New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Undefined pointer comparison in macro.c #1602
Comments
That is probably undefined behavior in C11 but AIUI not in C99, which rpm uses. |
It is undefined in C99; see http://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf, §6.3.2.3:
So I still believe it should be fixed.
This is undefined behavior in C99, and includes a reproducible test case. I consider that to be a concrete bug. |
Maybe I'm dense, but isn't q incremented in line 221 before the pointer comparison is done? |
It gets decremented again on line 227. |
So the initialization is not the problem? And a fix would be do change the for loop to:
|
The initialization is already undefined behavior. |
The
Panu, do you agree? |
I also don't think rpm will work correctly on EBCDIC platforms. |
Of course the initialization can be easily fixed by doing a
at the top of the do loop and
at the bottom. That way, q can be initialized to buf. (It will be decremented to buf-1 for empty lines nevertheless.) |
Regarding my comment about the wrong nb check: The following macro definition works:
But this produces an error:
|
My suggested fix was wrong, it should be:
Back to the pointer comparison: an even saner approach for this issue would be to let q point to the newline and not the char before the newline. |
This is actually a wonderful example: just like compiler warnings, undefined behavior is best viewed as a canary that points to buggy code, rather than flaw in itself. Much, much damage to codebases around the world is done by "fixing" compiler warnings, and undefined behavior is not any different. @mlschroe 's version fixes an ages old, very concrete bug (even an RFE) in the macro parser. It wasn't misbehaving because of the allegedly undefined behavior but because it was just a piece of bad old code, led to by the undefined behavior alert. Fixed by #1606 |
GCC has
-fsanitize=pointer-compare
, which adds instrumentation to detect invalid pointer comparisons. When built with this flag, and run withASAN_OPTIONS=detect_invalid_pointer_pairs=1
, virtually the entire testsuite fails due to an undefined pointer comparison inrpmio/macro.c
. This is in turn due toq
being initialized as one before the beginning of an allocated region.Reproduction script below. PR coming.
build-rpm.gz
The text was updated successfully, but these errors were encountered: