public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-9397] Avoid expecting nonzero size for access none void* arguments [PR101751].
@ 2021-12-16 23:04 Martin Sebor
  0 siblings, 0 replies; only message in thread
From: Martin Sebor @ 2021-12-16 23:04 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:5635c975576604afda35543f672c2cad79cb0046

commit r11-9397-g5635c975576604afda35543f672c2cad79cb0046
Author: Martin Sebor <msebor@redhat.com>
Date:   Thu Dec 9 11:24:14 2021 -0700

    Avoid expecting nonzero size for access none void* arguments [PR101751].
    
    Resolves:
    PR middle-end/101751 - attribute access none with void pointer expects nonzero size
    
    gcc/ChangeLog:
    
            PR middle-end/101751
            * doc/extend.texi (attribute access): Adjust.
            * calls.c (maybe_warn_rdwr_sizes): Treat access mode none on a void*
            argument as expecting as few as zero bytes.
    
    gcc/testsuite/ChangeLog:
    
            PR middle-end/101751
            * gcc.dg/Wstringop-overflow-86.c: New test.

Diff:
---
 gcc/calls.c                                  |  4 ++
 gcc/doc/extend.texi                          |  5 ++-
 gcc/testsuite/gcc.dg/Wstringop-overflow-86.c | 63 ++++++++++++++++++++++++++++
 3 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/gcc/calls.c b/gcc/calls.c
index 870b9ac7776..7d908c6a62b 100644
--- a/gcc/calls.c
+++ b/gcc/calls.c
@@ -2022,6 +2022,10 @@ maybe_warn_rdwr_sizes (rdwr_map *rwm, tree fndecl, tree fntype, tree exp)
 	  if (access.second.minsize
 	      && access.second.minsize != HOST_WIDE_INT_M1U)
 	    access_size = build_int_cstu (sizetype, access.second.minsize);
+	  else if (VOID_TYPE_P (argtype) && access.second.mode == access_none)
+	    /* Treat access mode none on a void* argument as expecting
+	       as little as zero bytes.  */
+	    access_size = size_zero_node;
 	  else
 	    access_size = size_one_node;
 	}
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 1acfaf1d345..689ec7de4d3 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -2487,7 +2487,6 @@ The following attributes are supported on most targets.
 @table @code
 @c Keep this table alphabetized by attribute name.  Treat _ as space.
 
-@item access
 @itemx access (@var{access-mode}, @var{ref-index})
 @itemx access (@var{access-mode}, @var{ref-index}, @var{size-index})
 
@@ -2561,7 +2560,9 @@ __attribute__ ((access (write_only, 1, 2), access (read_write, 3))) int fgets (c
 The access mode @code{none} specifies that the pointer to which it applies
 is not used to access the referenced object at all.  Unless the pointer is
 null the pointed-to object must exist and have at least the size as denoted
-by the @var{size-index} argument.  The object need not be initialized.
+by the @var{size-index} argument.  When the optional @var{size-index}
+argument is omitted for an argument of @code{void*} type the actual pointer
+agument is ignored.  The referenced object need not be initialized.
 The mode is intended to be used as a means to help validate the expected
 object size, for example in functions that call @code{__builtin_object_size}.
 @xref{Object Size Checking}.
diff --git a/gcc/testsuite/gcc.dg/Wstringop-overflow-86.c b/gcc/testsuite/gcc.dg/Wstringop-overflow-86.c
new file mode 100644
index 00000000000..345abe4a274
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/Wstringop-overflow-86.c
@@ -0,0 +1,63 @@
+/* PR middle-end/101751 - attribute access none with void pointer expects
+   nonzero size
+   { dg-do compile }
+   { dg-options "-Wall" } */
+
+__attribute__ ((access (none, 1))) void
+fvp_m1 (const void*);
+
+void nowarn_m1 (void)
+{
+  /* Verify these don't trigger a warning for calls to a function
+     declared with attribute access none.  */
+  fvp_m1 ((void*)-1);         // { dg-bogus "-Wstringop-" }
+  fvp_m1 ((void*)1);          // { dg-bogus "-Wstringop-" }
+}
+
+
+__attribute__ ((access (none, 1))) void
+fvp_none (void*);
+
+void nowarn_c_cp1 (void)
+{
+  char c;
+  fvp_none (&c);
+  fvp_none (&c + 1);          // { dg-bogus "-Wstringop-" }
+}
+
+void nowarn_f_fp1 (void)
+{
+  fvp_none ((char*)&nowarn_f_fp1);
+  fvp_none ((char*)&nowarn_f_fp1 + 1);
+}
+
+void nowarn_sp1_sp_4 (void)
+{
+  fvp_none ("" + 1);          // { dg-bogus "-Wstringop-" }
+  fvp_none ("123" + 4);       // { dg-bogus "-Wstringop-" }
+}
+
+
+__attribute__ ((access (none, 1))) void
+wfvp_none (void*);            // { dg-message "in a call to function 'wfvp_none' declared with attribute 'access \\\(none, 1\\\)'" }
+
+void warn_cm1_p1 (void)
+{
+  char c;
+  /* With optimization both of the following are diagnosed by -Warray-bounds.
+     The second also without optimization by -Wstringop-overread.  They
+     should both be diagnosed by the same warning even without optimization. */
+  wfvp_none (&c - 1);         // { dg-warning "" "pr??????" { xfail *-*-* } }
+  wfvp_none (&c + 2);         // { dg-warning "" }
+}
+
+void warn_fp2 (void)
+{
+  void *p = (char*)&warn_fp2 + sizeof warn_fp2;
+  fvp_none (p);               // { dg-warning "" "pr??????" { xfail *-*-* } }
+}
+
+void warn_sp2 (void)
+{
+  wfvp_none ("" + 2);         // { dg-warning "" }
+}


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

only message in thread, other threads:[~2021-12-16 23:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-16 23:04 [gcc r11-9397] Avoid expecting nonzero size for access none void* arguments [PR101751] 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).