[PATCH] c-attribs.c: Fix use of uninitialized variable nunits

Stefan Schulze Frielinghaus stefansf@linux.ibm.com
Tue Apr 28 18:27:11 GMT 2020


On Tue, Apr 28, 2020 at 11:33:31AM +0200, Richard Biener wrote:
> On Tue, Apr 28, 2020 at 10:03 AM Stefan Schulze Frielinghaus via
> Gcc-patches <gcc-patches@gcc.gnu.org> wrote:
> >
> > In function handle_vector_size_attribute local variable nunits is
> > supposed to be initialized by function type_valid_for_vector_size.
> > However, in case ARGS is null the function may return with a non-null
> > value and leave nunits uninitialized.  This results in warning/error:
> >
> > gcc/poly-int.h: In function 'tree_node* handle_vector_size_attribute(tree_node**, tree, tree, int, bool*)':
> > gcc/poly-int.h:330:3: error: 'nunits' may be used uninitialized in this function [-Werror=maybe-uninitialized]
> >   330 |   ((void) (&(RES).coeffs[0] == (C *) 0), \
> >       |   ^
> > gcc/c-family/c-attribs.c:3695:26: note: 'nunits' was declared here
> >  3695 |   unsigned HOST_WIDE_INT nunits;
> >       |
> >
> > This is fixed by also checking whether ARGS is null or not.
> >
> > Bootstrapped and regtested on S/390. Ok for master?
> 
> I think it's better to assert that it is not null for example by adding a
> nonnull attribute?  Can you check if that works?  If it doesn't the
> patch is OK.

Yes, that works, too.  Please find an updated version attached.  If you
think it is useful I could also add a gcc_assert (!args) for minimal
testing.
-------------- next part --------------
In function handle_vector_size_attribute local variable nunits is
supposed to be initialized by function type_valid_for_vector_size.
However, in case ARGS is null the function may return with a non-null
value and leave nunits uninitialized.  This results in warning/error:

gcc/poly-int.h: In function 'tree_node* handle_vector_size_attribute(tree_node**, tree, tree, int, bool*)':
gcc/poly-int.h:330:3: error: 'nunits' may be used uninitialized in this function [-Werror=maybe-uninitialized]
  330 |   ((void) (&(RES).coeffs[0] == (C *) 0), \
      |   ^
gcc/c-family/c-attribs.c:3695:26: note: 'nunits' was declared here
 3695 |   unsigned HOST_WIDE_INT nunits;
      |

Added attribute nonnull for argument args in order to state the
invariant and to silence warning.

gcc/c-family/ChangeLog:

2020-04-28  Stefan Schulze Frielinghaus  <stefansf@linux.ibm.com>

	* c-attribs.c (handle_vector_size_attribute): Add attribute
	nonnull for argument args in order to state invariant and to
	silence warning of uninitialized variable usage.
---
 gcc/c-family/c-attribs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gcc/c-family/c-attribs.c b/gcc/c-family/c-attribs.c
index ac936d5bbbb..e49a74c4048 100644
--- a/gcc/c-family/c-attribs.c
+++ b/gcc/c-family/c-attribs.c
@@ -117,7 +117,7 @@ static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
 static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
 static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
 static tree handle_vector_size_attribute (tree *, tree, tree, int,
-					  bool *);
+					  bool *) ATTRIBUTE_NONNULL(3);
 static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
 static tree handle_nonstring_attribute (tree *, tree, tree, int, bool *);
 static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
-- 
2.25.3



More information about the Gcc-patches mailing list