Make it possible to pass NULL through variadic functions on platforms
where NULL has an integer type that is more narrow than a pointer. On
such platforms we widen null pointer constants to a pointer-sized
integer.
Fixes PR20949.
Differential D5480
Promote NPCs used as arguments to variadic functions rnk on Sep 24 2014, 9:16 AM. Authored by
Details Make it possible to pass NULL through variadic functions on platforms Fixes PR20949.
Diff Detail
Event TimelineComment Actions Note that Linux headers sometimes define NULL to 0 on x86_64, which causes the same problem. This change will "fix" the problem by widening the int to an intptr_t. Comment Actions lgtm, thanks! For 32bit, this should never make a difference in practice. For 64bit, passing "0" to a varargs function will now be a 8 byte write instead of a 4 byte write, but arguments are 8-byte aligned there, and nothing should break if the padding bytes for 0s are now zero instead of random (and for pointers, it helps). Comment Actions It's usually best to throw out a Phab review & start again if you don't cc Comment Actions Perhaps a warning should be added so code doesn't "miscompile" when compiled with GCC? Is there a reason to restrict this just to null pointer constants?
Comment Actions I don't think the warning is actionable for the user. How do we avoid false positives on printf("%d\n", 0)? Users are typically saavy enough to know that passing '0' to a varargs function will be interpreted as an int, or at least be able to debug the crash once it happens. The subtle case is when passing NULL, which 99% of the time just works with GCC's stddef.h. In the 1% corner case where NULL is defined incorrectly (literal 0 and not 0L), I can live the fact that clang and gcc will have subtle behavior differences.
I don't see why we should implicitly widen all ints to i64 in varargs contexts. Comment Actions I wonder whether it makes sense to restrict this to 64-bit MSVC platforms, since it's not really a problem elsewhere (other platforms have a sane NULL) and it presumably generates marginally worse code. Plus if we start doing this on, say, Mac OS, it'll make it harder for people to spot they have a bug until they port to another system.
Comment Actions OK, but there are other triples (WoA) that use the itanium environment that might have broken headers, so how about Windows platforms where pointer size != integer size? (aarch64, x86_64, ppc64)
|