Skip to content

UBSAN should catch undefined behavior in realloc #113065

@bhaible

Description

@bhaible

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions