]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Check to see if null pointer is dereferenceable [PR102630].
authorMartin Sebor <msebor@redhat.com>
Wed, 13 Oct 2021 16:31:37 +0000 (10:31 -0600)
committerMartin Sebor <msebor@redhat.com>
Wed, 13 Oct 2021 16:31:37 +0000 (10:31 -0600)
Resolves:
PR middle-end/102630 - Spurious -Warray-bounds with named address space

gcc/ChangeLog:

PR middle-end/102630
* pointer-query.cc (compute_objsize_r): Handle named address spaces.

gcc/testsuite/ChangeLog:

PR middle-end/102630
* gcc.target/i386/addr-space-2.c: Add -Wall.
* gcc.target/i386/addr-space-3.c: New test.

gcc/pointer-query.cc
gcc/testsuite/gcc.target/i386/addr-space-2.c
gcc/testsuite/gcc.target/i386/addr-space-3.c [new file with mode: 0644]

index 83b1f0fc8664cdf8efc529cafedcc4bbfc349d3b..910f452868ef786a01427e6e77cf6113e6b85975 100644 (file)
@@ -41,6 +41,7 @@
 #include "pointer-query.h"
 #include "tree-pretty-print.h"
 #include "tree-ssanames.h"
+#include "target.h"
 
 static bool compute_objsize_r (tree, int, access_ref *, ssa_name_limit_t &,
                               pointer_query *);
@@ -1869,13 +1870,24 @@ compute_objsize_r (tree ptr, int ostype, access_ref *pref,
   if (code == INTEGER_CST)
     {
       /* Pointer constants other than null are most likely the result
-        of erroneous null pointer addition/subtraction.  Set size to
-        zero.  For null pointers, set size to the maximum for now
-        since those may be the result of jump threading.  */
+        of erroneous null pointer addition/subtraction.  Unless zero
+        is a valid address set size to zero.  For null pointers, set
+        size to the maximum for now since those may be the result of
+        jump threading.  */
       if (integer_zerop (ptr))
        pref->set_max_size_range ();
+      else if (POINTER_TYPE_P (TREE_TYPE (ptr)))
+       {
+         tree deref_type = TREE_TYPE (TREE_TYPE (ptr));
+         addr_space_t as = TYPE_ADDR_SPACE (deref_type);
+         if (targetm.addr_space.zero_address_valid (as))
+           pref->set_max_size_range ();
+         else
+           pref->sizrng[0] = pref->sizrng[1] = 0;
+       }
       else
        pref->sizrng[0] = pref->sizrng[1] = 0;
+
       pref->ref = ptr;
 
       return true;
index d5c24b61096f872f0bbb9957390935f5b15da400..974436855372e4431289627956fda5732ee365d5 100644 (file)
@@ -1,10 +1,11 @@
 /* { dg-do compile } */
-/* { dg-options "-O" } */
+/* { dg-options "-O -Wall" } */
 /* { dg-final { scan-assembler "fs:16" } } */
 /* { dg-final { scan-assembler "gs:16" } } */
 
 int test(void)
 {
+  /* Also verify the accesses don't trigger warnings.  */
   int __seg_fs *f = (int __seg_fs *)16;
   int __seg_gs *g = (int __seg_gs *)16;
   return *f + *g;
diff --git a/gcc/testsuite/gcc.target/i386/addr-space-3.c b/gcc/testsuite/gcc.target/i386/addr-space-3.c
new file mode 100644 (file)
index 0000000..cf0f400
--- /dev/null
@@ -0,0 +1,17 @@
+/* PR middle-end/102630 - Spurious -Warray-bounds with named address space
+  { dg-do compile }
+  { dg-options "-O -Wall" }
+  { dg-final { scan-assembler "fs:0" } }
+  { dg-final { scan-assembler "gs:0" } } */
+
+void test_fs_null_store (void)
+{
+  int __seg_fs *fs = (int __seg_fs *)0;
+  *fs = 1;
+}
+
+void test_gs_null_store (void)
+{
+  int __seg_gs *gs = (int __seg_gs *)0;
+  *gs = 2;
+}