Skip to content

Commit c2979c5

Browse files
authored
[Clang] Add release note for pointer overflow optimization change (#122462)
Add a release note for optimization change related to pointer overflow checks. I've put this in the breaking changes section to give it the best chance of being seen.
1 parent 16923da commit c2979c5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,29 @@ code bases.
5858
containing strict-aliasing violations. The new default behavior can be
5959
disabled using ``-fno-pointer-tbaa``.
6060

61+
- Clang will now more aggressively use undefined behavior on pointer addition
62+
overflow for optimization purposes. For example, a check like
63+
``ptr + unsigned_offset < ptr`` will now optimize to ``false``, because
64+
``ptr + unsigned_offset`` will cause undefined behavior if it overflows (or
65+
advances past the end of the object).
66+
67+
Previously, ``ptr + unsigned_offset < ptr`` was optimized (by both Clang and
68+
GCC) to ``(ssize_t)unsigned_offset < 0``. This also results in an incorrect
69+
overflow check, but in a way that is less apparent when only testing with
70+
pointers in the low half of the address space.
71+
72+
To avoid pointer addition overflow, it is necessary to perform the addition
73+
on integers, for example using
74+
``(uintptr_t)ptr + unsigned_offset < (uintptr_t)ptr``. Sometimes, it is also
75+
possible to rewrite checks by only comparing the offset. For example,
76+
``ptr + offset < end_ptr && ptr + offset >= ptr`` can be written as
77+
``offset < (uintptr_t)(end_ptr - ptr)``.
78+
79+
Undefined behavior due to pointer addition overflow can be reliably detected
80+
using ``-fsanitize=pointer-overflow``. It is also possible to use
81+
``-fno-strict-overflow`` to opt-in to a language dialect where signed integer
82+
and pointer overflow are well-defined.
83+
6184
C/C++ Language Potentially Breaking Changes
6285
-------------------------------------------
6386

0 commit comments

Comments
 (0)