aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/Wdangling-pointer.c
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/testsuite/gcc.dg/Wdangling-pointer.c')
-rw-r--r--gcc/testsuite/gcc.dg/Wdangling-pointer.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/gcc/testsuite/gcc.dg/Wdangling-pointer.c b/gcc/testsuite/gcc.dg/Wdangling-pointer.c
new file mode 100644
index 0000000..d792d09
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wdangling-pointer.c
@@ -0,0 +1,75 @@
+/* Exercise basic C-only cases of -Wdangling-pointer.
+ { dg-do compile }
+ { dg-options "-O0 -Wall" } */
+
+typedef __SIZE_TYPE__ size_t;
+
+extern void* memchr (const void*, int, size_t);
+extern char* strchr (const char*, int);
+
+void sink (const void*, ...);
+
+
+void nowarn_compound_literal (int i)
+{
+ {
+ int *p = (int[]){ 1, 2, 3 };
+ sink (p);
+ }
+ {
+ int *q = (int[]){ 1, 2, 3 };
+ int *p = &q[1];
+ sink (p);
+ }
+ {
+ int *p = __builtin_memchr ((int[]){ 1, 2, 3 }, 2, 3 * sizeof *p);
+ sink (p);
+ }
+ {
+ int *p = __builtin_memchr ((int[]){ i, i + 1 }, 3, 2 * sizeof *p);
+ sink (p);
+ }
+}
+
+
+void warn_compound_literal (int i)
+{
+ int *p;
+ {
+ p = (int[]){ 1, 2, 3 }; // { dg-message "unnamed temporary" },
+ }
+ sink (p); // { dg-warning "using dangling pointer 'p' to an unnamed temporary" }
+
+ {
+ int *q =
+ (int[]){ 1, 2, 3 }; // { dg-message "unnamed temporary" },
+ p = &q[1];
+ }
+ sink (p); // { dg-warning "using dangling pointer 'p' to an unnamed temporary" }
+ {
+ p = (int*)memchr (
+ (int[]){ 1, 2, 3 }, // { dg-message "unnamed temporary" }
+ 2, 3 * sizeof *p);
+ }
+ sink (p); // { dg-warning "using dangling pointer 'p' to an unnamed temporary" }
+
+ {
+ p = (int*)memchr (
+ (int[]){ i, i + 1 },// { dg-message "unnamed temporary" }
+ 3, 2 * sizeof *p);
+ }
+ sink (p); // { dg-warning "using dangling pointer 'p' to an unnamed temporary" }
+}
+
+
+void warn_store_compound_literal (int **p)
+{
+ int *q = (int[]) { 1, 2, 3 };
+ p[0] = q; // { dg-warning "storing the address" }
+}
+
+void warn_store_vla (int n, int **p)
+{
+ int a[n];
+ p[1] = &a[1]; // { dg-warning "-Wdangling-pointer" "pr??????" { xfail *-*-* } }
+}