char* pointer comparisons and clang12

Joseph Myers joseph@codesourcery.com
Tue May 18 18:12:34 GMT 2021


On Tue, 11 May 2021, Alan Modra via Binutils wrote:

> Is it true that char* pointers cannot be compared using the relational 
> operators <, >, <=, or >= except when they point into the same object
> or array (or one past)?  Reading the C standard section 6.5.8 seems to
> say that.  "In all other cases, the behavior is undefined."  (I know
> enough computer architecture to know why the standard is written that
> way, so the question is rhetorical.  The real question is below.)
> 
> Are there compilers that optimize making use of that UB on flat memory
> architectures?  Specifically, clang12.  See pr27836.
> 
> We use pointer comparisons like the one at binutils/dwarf.c:8802 in
> many places in binutils to test a value derived from user imput
> against an array bound.  If this UB is a concern it seems like all of
> those tests will need rewriting.

There is some discussion at 
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91227> concerning possible 
optimizations in the case where the compiler can see (possibly after 
inlining / LTO) that one pointer is to a local variable, and suggesting 
converting to uintptr_t before comparison to avoid such optimizations.

-- 
Joseph S. Myers
joseph@codesourcery.com


More information about the Binutils mailing list