<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/113065>113065</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            UBSAN should catch undefined behavior in realloc
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          bhaible
      </td>
    </tr>
</table>

<pre>
    ISO C 23 ยง 7.24.3.7 specifies that `realloc (ptr, 0)`, when `ptr` is non-null, is "undefined behavior".

It would be very useful if the UBSAN would catch such invocations, because this corner of realloc's specification is a real portability hassle, cf. https://sourceware.org/bugzilla/show_bug.cgi?id=12547 .

How to reproduce:
========================== foo.c =========================
```
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int main ()
{
  char *p = malloc (200);
  printf ("%p", p);
  errno = 0;
  char *q = realloc (p, 0);
  printf (" %p %d\n", q, errno);
}
```
```
$ clang -Wall -fsanitize=undefined -std=gnu23 foo.c
$ ASAN_OPTIONS="abort_on_error=0 allocator_may_return_null=1" UBSAN_OPTIONS= ./a.out 
0x55fd545a9300 (nil) 0
$ ASAN_OPTIONS="abort_on_error=0 allocator_may_return_null=0" UBSAN_OPTIONS= ./a.out 
0x5620c7431300 (nil) 0
```

Expected: Some error report from UBSAN.

Seen with clang version 19.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVU1v4zYQ_TXUZWCBIiXLPujg2Gs0l6SAW_RoUNTIYkGTWn7Ym_31BSUncbYN0EMAgZL9Zua9Gc2MhPfqZBAbUj2QapeJGAbrmnYQqtWYtbZ7aR4Pz7AFxoFsGdnUUOeszHlegx9Rql6hhzCIAGRJHQqtrQTCVmNwhG2BErYmS5oerwOaZJSQJQXlwVizMFHrhCoPhLFoOuyVwQ5aHMRFWUcYywndEbqZz8cAVxt1MoALuheIHvuoQfUQBoQ_Hw6bp5uF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