Menu

#3467 SDCC's definition of NULL is non-compliant

closed-fixed
None
Preprocessor
5
2022-08-06
2022-08-05
No

NULL, along with all other object-like macros defined by the C standard, is mandated to be protected by parentheses:

Any definition of an object-like macro described in this clause shall expand to code that is fully protected by parentheses where necessary, so that it groups in an arbitrary expression as if it were a single identifier.

SDCC's definition of NULL appears to fail in this regard:

sdcc/trunk/sdcc/device/include/stddef.h: #define NULL (void *)0

Although this may seem impossible to observe, it is technically possible to construct a program which is impacted by this:

#include <stdio.h>
#include <stddef.h>

int arr[1];

int main()
{
     printf("%d\n", _Generic(NULL[arr], int: 1, void *: 2, default: 3));
}

A standard-conforming implementation has, with this program as input, only two possibilities as to the output of the program:

  • Printing 1
  • Failing to compile the program

SDCC's definition of NULL is such that this program prints 2.

Discussion

  • Benedikt Freisen

    • status: open --> closed-fixed
    • assigned_to: Benedikt Freisen
     
  • Benedikt Freisen

    Fixed in [r13639]. IMO it is trivial enough to omit adding a test case.

     

    Related

    Commit: [r13639]

  • Philipp Klaus Krause

    Thanks for finding this long-standing bug in legacy NULL. Feel free to also have a look at nullptr, which was implemented less than a week ago, and probably has more remaining bugs.

     

Log in to post a comment.

MongoDB Logo MongoDB