On Tue, 24 Sep 2019, Jakub Jelinek wrote:

> Hi!
> 
> build_array_type_nelts is only meaningful for non-zero number of elements,
> for 0 it creates weirdo arrays like char D.2358[0:18446744073709551615].
> The following patch uses in that case types like the C FE emits for
> zero-length array instead (i.e. char D.2358[0:] with forced 0 size).
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

Not sure [0:-1] is actually the canonical zero-length array (and IIRC
what the C++ FE creates and what layout_type can lay out).  So why
not fix the sanitizers instead?

Richard.

> 2019-09-24  Jakub Jelinek  <ja...@redhat.com>
> 
>       PR sanitizer/91707
>       * tree-ssa-ccp.c (fold_builtin_alloca_with_align): For n_elem 0
>       use a type like C zero length array instead of array from 0
>       to SIZE_MAX.
> 
> --- gcc/tree-ssa-ccp.c.jj     2019-09-20 12:25:26.809718354 +0200
> +++ gcc/tree-ssa-ccp.c        2019-09-23 19:38:03.530722874 +0200
> @@ -2223,7 +2223,18 @@ fold_builtin_alloca_with_align (gimple *
>    /* Declare array.  */
>    elem_type = build_nonstandard_integer_type (BITS_PER_UNIT, 1);
>    n_elem = size * 8 / BITS_PER_UNIT;
> -  array_type = build_array_type_nelts (elem_type, n_elem);
> +  if (n_elem == 0)
> +    {
> +      /* For alloca (0), use array type similar to C zero-length arrays.  */
> +      tree range_type = build_range_type (sizetype, size_zero_node, 
> NULL_TREE);
> +      array_type = build_array_type (elem_type, range_type);
> +      array_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (array_type));
> +      TYPE_SIZE (array_type) = bitsize_zero_node;
> +      TYPE_SIZE_UNIT (array_type) = size_zero_node;
> +      SET_TYPE_STRUCTURAL_EQUALITY (array_type);
> +    }
> +  else
> +    array_type = build_array_type_nelts (elem_type, n_elem);
>    var = create_tmp_var (array_type);
>    SET_DECL_ALIGN (var, TREE_INT_CST_LOW (gimple_call_arg (stmt, 1)));
>    if (uid != 0)
> 
>       Jakub
> 

-- 
Richard Biener <rguent...@suse.de>
SUSE Software Solutions Germany GmbH, Maxfeldstrasse 5, 90409 Nuernberg,
Germany; GF: Felix Imendörffer; HRB 247165 (AG München)

Reply via email to