aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorMartin Sebor <msebor@redhat.com>2021-10-13 10:31:37 -0600
committerMartin Sebor <msebor@redhat.com>2021-10-13 10:31:37 -0600
commit54fa5567a27eb7ee72cd2321d0291c8a9b436ce9 (patch)
tree2bfac9e3baedfd42699bb4d29b954eb0c9216d6b /gcc
parent43ae43f654749d291d871ca6ef7c96ea16580fad (diff)
downloadgcc-54fa5567a27eb7ee72cd2321d0291c8a9b436ce9.zip
gcc-54fa5567a27eb7ee72cd2321d0291c8a9b436ce9.tar.gz
gcc-54fa5567a27eb7ee72cd2321d0291c8a9b436ce9.tar.bz2
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.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/pointer-query.cc18
-rw-r--r--gcc/testsuite/gcc.target/i386/addr-space-2.c3
-rw-r--r--gcc/testsuite/gcc.target/i386/addr-space-3.c17
3 files changed, 34 insertions, 4 deletions
diff --git a/gcc/pointer-query.cc b/gcc/pointer-query.cc
index 83b1f0f..910f452 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 d5c24b6..9744368 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 0000000..cf0f400
--- /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;
+}