You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Critically, glibc's malloc unconditionally fails for requests larger than PTRDIFF_MAX, so, with glibc, the above function test would always return 0. However, on all nonzero optimization levels, Clang assumes that the call to malloc will unconditionally succeed, effectively deleting the if-statement. As a result, the function invokes undefined behavior by computing a pointer subtraction whose result overflows (since TOO_LARGE_SIZE > PTRDIFF_MAX). If the if-statement were not deleted, the body of the if-statement would always be executed, thereby avoiding invoking undefined behavior.
On all nonzero optimization levels, Clang generates (x86_64, Intel syntax) assembly similar to the following:
test: movabs rax,-9223372036854775808ret
Host system type: Arch Linux, x86_64 (with glibc, of course).
Clang version: official Arch Linux package clang 18.1.8-5.
The text was updated successfully, but these errors were encountered:
Optimization of
malloc
andfree
calls can produce undefined behavior.Consider this minimal test case:
Critically, glibc's
malloc
unconditionally fails for requests larger thanPTRDIFF_MAX
, so, with glibc, the above functiontest
would always return0
. However, on all nonzero optimization levels, Clang assumes that the call tomalloc
will unconditionally succeed, effectively deleting theif
-statement. As a result, the function invokes undefined behavior by computing a pointer subtraction whose result overflows (sinceTOO_LARGE_SIZE
>PTRDIFF_MAX
). If theif
-statement were not deleted, the body of theif
-statement would always be executed, thereby avoiding invoking undefined behavior.On all nonzero optimization levels, Clang generates (x86_64, Intel syntax) assembly similar to the following:
Host system type: Arch Linux, x86_64 (with glibc, of course).
Clang version: official Arch Linux package clang 18.1.8-5.
The text was updated successfully, but these errors were encountered: