<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/121176>121176</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Incorrect Optimization of malloc Calls: Erroneous Assumption of Unconditional Success of malloc
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
jonathan-gruber-jg
</td>
</tr>
</table>
<pre>
Given calls to `malloc` that can potentially fail, Clang incorrectly assumes they will unconditionally succeed. In some cases, Clang even elides such calls to `malloc` and optimizes the surrounding code around the assumption that the call(s) to `malloc` would unconditionally succeed if actually executed.
```c
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#define TOO_LARGE_SIZE ((size_t)PTRDIFF_MAX + 1) /* Request size greater than PTRDIFF_MAX. */
int test_too_big_malloc() {
int ret;
void *const p = malloc(TOO_LARGE_SIZE);
ret = p != NULL;
free(p);
return ret;
}
```
On all the (nonzero) optimization levels that I tested, Clang generates x86_64 assembly (Intel syntax) similar to the following, where it not only completely elides the calls to `malloc` and `free` but assumes that the call to `malloc` would have succeeded if actually executed:
```asm
test_too_big_malloc:
mov eax, 1
ret
```
glibc's `malloc` _unconditionally_ fails for requests of sizes greater than `PTRDIFF_MAX`, so Clang compiles the above test case incorrectly for glibc in particular.
Host system: Arch Linux, x86_64.
Clang version: official Arch Linux package of clang, version 18.1.8-5.
Command line to reproduce results: clang -c <file>.c -O<opt-level>, where <opt-level> is g, 1, 2, 3, s, or z.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJx0VU1v4zgP_jXKhYhhy6kTH3xI02beAH23i_kAFnsJZJm2NStLXklOJ_31Cypum85MAcOJRfLh10NKeK86g1ixm1t2c7cQU-itq75bI0IvzLJzU41u-b1b1LY5V5_UCQ1IobWHYIEV6SC0tpIVKYReBJDCwGgDmqCE1mdohdKM72CnhelAGWmdQxn0GYT304AeQ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