1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-28 06:30:16 +00:00
cc65/test/val/nullptr.c
Kugel Fuhr cd4357057f The change from #2495 didn't take into account that recursive calls to main()
are legal in C. With the changes from #2495, such calls will usually crash the
machine. But recursive calls to main() are rare and on the 6502 every byte
saved is precious. So this change limits the effect of #2495 to cc65 mode and
at the same time disallows recursive calls to main() in this mode. If
recursive calls to main() are actually required, the code must be compiled in
c89 or c99 mode.
2024-09-02 10:39:42 +02:00

74 lines
1.6 KiB
C

/* Bug # - Pointer compared to null pointer constant */
#include <stdio.h>
unsigned failures;
struct S {
char a[4];
} *p;
#define TEST_NULL(E) \
do { \
a = (E) == 0 && !(E); \
if (!a) \
{ \
++failures; \
printf("failed: " #E " should be null\n"); \
} \
} while(0);
#define TEST_NON_NULL(E) \
do { \
a = (E) != 0 && !!(E) && (E); \
if (!a) \
{ \
++failures; \
printf("failed: " #E " should be non-null\n"); \
} \
} while(0);
void func() { }
int main()
{
int a;
/* Null pointer constant (per ISO C) compared equal to null pointer constant */
TEST_NULL((void*)0)
/* Null pointer compared equal to null pointer constant */
TEST_NULL((char*)0)
/* Null pointer obtained with -> */
TEST_NULL(((struct S*)0)->a)
/* Null pointer obtained with -> */
TEST_NULL(p->a)
/* Null pointer obtained with cast and -> */
TEST_NULL(((struct S*)(a = 0))->a)
/* Null pointer obtained with cast and -> */
TEST_NULL((a = 0, ((struct S*)a)->a))
/* Non-null pointer obtained with cast and -> */
TEST_NON_NULL(((struct S*)(long)(a = 0x1234))->a)
/* Non-null pointer obtained with cast and -> */
TEST_NON_NULL((a = 0x1234, ((struct S*)a)->a))
/* Non-null pointer obtained with cast and -> */
TEST_NON_NULL(((struct S*)&a)->a)
/* Non-null pointer obtained with cast and -> */
TEST_NON_NULL(((struct S*)&func)->a)
if (failures != 0)
{
printf("failures: %u\n", failures);
}
return failures;
}