From: Martin Sebor Date: Wed, 13 Oct 2021 16:31:37 +0000 (-0600) Subject: Check to see if null pointer is dereferenceable [PR102630]. X-Git-Tag: upstream/12.2.0~4321 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=54fa5567a27eb7ee72cd2321d0291c8a9b436ce9;p=platform%2Fupstream%2Fgcc.git 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 --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; +}