public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-4376] Check to see if null pointer is dereferenceable [PR102630].
@ 2021-10-13 16:34 Martin Sebor
  0 siblings, 0 replies; only message in thread
From: Martin Sebor @ 2021-10-13 16:34 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:54fa5567a27eb7ee72cd2321d0291c8a9b436ce9

commit r12-4376-g54fa5567a27eb7ee72cd2321d0291c8a9b436ce9
Author: Martin Sebor <msebor@redhat.com>
Date:   Wed Oct 13 10:31:37 2021 -0600

    Check to see if null pointer is dereferenceable [PR102630].
    
    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.

Diff:
---
 gcc/pointer-query.cc                         | 18 +++++++++++++++---
 gcc/testsuite/gcc.target/i386/addr-space-2.c |  3 ++-
 gcc/testsuite/gcc.target/i386/addr-space-3.c | 17 +++++++++++++++++
 3 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/gcc/pointer-query.cc b/gcc/pointer-query.cc
index 83b1f0fc866..910f452868e 100644
--- a/gcc/pointer-query.cc
+++ b/gcc/pointer-query.cc
@@ -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;
diff --git a/gcc/testsuite/gcc.target/i386/addr-space-2.c b/gcc/testsuite/gcc.target/i386/addr-space-2.c
index d5c24b61096..97443685537 100644
--- a/gcc/testsuite/gcc.target/i386/addr-space-2.c
+++ b/gcc/testsuite/gcc.target/i386/addr-space-2.c
@@ -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
index 00000000000..cf0f400de4d
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/addr-space-3.c
@@ -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;
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-10-13 16:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-13 16:34 [gcc r12-4376] Check to see if null pointer is dereferenceable [PR102630] Martin Sebor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).