* char* pointer comparisons and clang12
@ 2021-05-11 6:06 Alan Modra
2021-05-18 8:30 ` Jacob Lifshay
2021-05-18 18:12 ` Joseph Myers
0 siblings, 2 replies; 3+ messages in thread
From: Alan Modra @ 2021-05-11 6:06 UTC (permalink / raw)
To: binutils
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.
--
Alan Modra
Australia Development Lab, IBM
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: char* pointer comparisons and clang12
2021-05-11 6:06 char* pointer comparisons and clang12 Alan Modra
@ 2021-05-18 8:30 ` Jacob Lifshay
2021-05-18 18:12 ` Joseph Myers
1 sibling, 0 replies; 3+ messages in thread
From: Jacob Lifshay @ 2021-05-18 8:30 UTC (permalink / raw)
To: Alan Modra; +Cc: binutils
On Tue, May 18, 2021, 00:21 Alan Modra via Binutils <binutils@sourceware.org>
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.
>
Idk about when clang is in C mode, but at least for C++ mode clang gives
defined semantics to the < operator, since that's used to implement
std::less<T *>, which is required by the C++ standard to give a strict
total order on all pointers.
https://en.cppreference.com/w/cpp/utility/functional/less
libc++ 12 implementation of std::less:
https://github.com/llvm/llvm-project/blob/llvmorg-12.0.0/libcxx/include/__functional_base#L54
Jacob Lifshay
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: char* pointer comparisons and clang12
2021-05-11 6:06 char* pointer comparisons and clang12 Alan Modra
2021-05-18 8:30 ` Jacob Lifshay
@ 2021-05-18 18:12 ` Joseph Myers
1 sibling, 0 replies; 3+ messages in thread
From: Joseph Myers @ 2021-05-18 18:12 UTC (permalink / raw)
To: Alan Modra; +Cc: binutils
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-05-18 18:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-11 6:06 char* pointer comparisons and clang12 Alan Modra
2021-05-18 8:30 ` Jacob Lifshay
2021-05-18 18:12 ` Joseph Myers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).