Ping [PATCH] c++/42121 - diagnose invalid flexible array members

Jason Merrill jason@redhat.com
Fri Dec 4 17:51:00 GMT 2015


On 12/03/2015 11:42 PM, Martin Sebor wrote:
> +	  if (next && TREE_CODE (next) == FIELD_DECL)

This will break if there's a non-field between the array and the next field.

> @@ -4114,7 +4115,10 @@ walk_subobject_offsets (tree type,
>
>        /* Avoid recursing into objects that are not interesting.  */
>        if (!CLASS_TYPE_P (element_type)
> -	  || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
> +	  || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)
> +	  || !domain
> +	  /* Flexible array members have no upper bound.  */
> +	  || !TYPE_MAX_VALUE (domain))

Why is this desirable?  We do want to avoid empty bases at the same 
address as a flexible array of the same type.

> +	&& (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
> +	    || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type)));

This can be integer_zerop.

> +       *seen_field = *seen_field || field_nonempty_p (f), fld = next)

Please add parens around the || expression.

> +	  && !tree_int_cst_equal (size_max_node, TYPE_MAX_VALUE (dom)))

This can be integer_minus_onep or integer_all_onesp.

> +	     its fields.  The recursive call to the function will
> +	     either return 0 or the flexible array member whose

Let's say NULL_TREE here rather than 0.

> +  {
> +    bool dummy = false;
> +    check_flexarrays (t, TYPE_FIELDS (t), &dummy);
> +  }

This should be called from check_bases_and_members, or even integrated 
into check_field_decls.

> -	  else if (name)
> -	    pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);

Why?

> @@ -10912,11 +10916,19 @@ grokdeclarator (const cp_declarator *declarator,
>  	if (!staticp && TREE_CODE (type) == ARRAY_TYPE
>  	    && TYPE_DOMAIN (type) == NULL_TREE)
>  	  {
> -	    tree itype = compute_array_index_type (dname, integer_zero_node,
> +	    if (TREE_CODE (ctype) == UNION_TYPE
> +		|| TREE_CODE (ctype) == QUAL_UNION_TYPE)
> +	      {
> +		error ("flexible array member in union");
> +		type = error_mark_node;
> +	      }
> +	    else
> +	      {
> +		tree itype = compute_array_index_type (dname, NULL_TREE,
>  						       tf_warning_or_error);
>  		type = build_cplus_array_type (TREE_TYPE (type), itype);
>  	      }

Can we leave TYPE_DOMAIN null for flexible arrays so you don't need to 
add special new handling all over the place?

> -    tree decl;
> +    tree decl = NULL_TREE;

Why?

> +++ b/gcc/testsuite/g++.dg/cpp0x/bad_array_new2.C
> @@ -1,7 +1,16 @@
>  // Test for throwing bad_array_new_length on invalid array length
>  // { dg-do run { target c++11 } }
>
> -#include <new>
> +// #include <new>
> +
> +namespace std {
> +struct exception {
> +    virtual ~exception () { }
> +};
> +
> +struct bad_alloc: exception { };
> +struct bad_array_new_length { };
> +}

Why?

Jason



More information about the Gcc-patches mailing list