Skip to content

UndefinedBehaviorSanitizer should no longer report an error about adding a NULL pointer and a 0 integer #113062

Closed
@bhaible

Description

@bhaible

According to https://sourceware.org/pipermail/libc-alpha/2024-October/160375.html, N3322 has been accepted for inclusion in ISO C.

In particular, adding a NULL pointer and a 0 integer is no longer undefined behaviour. clang's UBSAN should therefore no longer report a "runtime error" about it.

How to reproduce:

  1. Save this file as n3322-1.c:
/* This program exercises functionality allowed by
   https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3322.pdf */

#include <stddef.h>

int main ()
{
  char *volatile p1 = NULL;
  char *volatile p2 = NULL;
  ptrdiff_t n = p1 - p2;
  char *q = p1 + n;
  (void) q;
}
  1. Compile it:
$ clang -fsanitize=undefined,address -O0 -fno-omit-frame-pointer -ggdb n3322-1.c
  1. Run it:
$ ASAN_OPTIONS="detect_leaks=0 abort_on_error=0 allocator_may_return_null=1" ./a.out
n3322-1.c:11:16: runtime error: applying zero offset to null pointer
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior n3322-1.c:11:16 

This runtime error, seen with clang 19, should go away.

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler-rt:ubsanUndefined behavior sanitizerconfirmedVerified by a second party

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions