aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/tree-ssa/wnull-dereference.c
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/testsuite/gcc.dg/tree-ssa/wnull-dereference.c')
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/wnull-dereference.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/wnull-dereference.c b/gcc/testsuite/gcc.dg/tree-ssa/wnull-dereference.c
new file mode 100644
index 0000000..db36acc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/wnull-dereference.c
@@ -0,0 +1,82 @@
+/* { dg-do compile } */
+/* PR c/16531 */
+/* { dg-options "-O2 -fdelete-null-pointer-checks -Wnull-dereference" } */
+/* { dg-skip-if "" keeps_null_pointer_checks } */
+
+#ifndef __cplusplus
+#define NULL (void *)0
+#else
+#define NULL nullptr
+#endif
+
+struct t
+{
+ int bar;
+};
+
+struct t2
+{
+ struct t *s;
+};
+
+void test1 ()
+{
+ struct t *s = NULL;
+ s->bar = 1; /* { dg-warning "null" } */
+}
+
+void test2 (struct t *s)
+{
+ if (s == NULL && s->bar > 2) /* { dg-warning "null" } */
+ return;
+
+ s->bar = 3;
+}
+
+void test3 (struct t *s)
+{
+ if (s != NULL || s->bar > 2) /* { dg-warning "null" } */
+ return;
+
+ s->bar = 3; /* { dg-warning "null" } */
+}
+
+int test4 (struct t *s)
+{
+ if (s != NULL && s->bar > 2) /* { dg-bogus "null" } */
+ return 1;
+ return 0;
+}
+
+int test5 (struct t *s)
+{
+ if (s == NULL || s->bar > 2) /* { dg-bogus "null" } */
+ return 1;
+ return 0;
+}
+
+int test6 (struct t2 *s)
+{
+ if (s->s == 0 && s->s->bar == 0) /* { dg-warning "null" } */
+ return 1;
+ return 0;
+}
+
+int test7 (struct t *s)
+{
+ s = 0;
+ return s->bar; /* { dg-warning "null" } */
+}
+
+int test8 ()
+{
+ return ((struct t *)0)->bar; /* { dg-warning "null" } */
+}
+
+void test9 (struct t **s)
+{
+ if (s == 0)
+ *s = 0; /* { dg-warning "null" } */
+}
+
+