public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Martin Sebor <msebor@gmail.com>
Cc: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] handle VLA of zero length arrays and vice versa (PR 99121)
Date: Wed, 17 Feb 2021 14:56:36 +0100	[thread overview]
Message-ID: <20210217135636.GN4020736@tucnak> (raw)
In-Reply-To: <0d2809b1-1dbb-772e-a33b-eb1b694c4d45@gmail.com>

On Tue, Feb 16, 2021 at 08:34:41PM -0700, Martin Sebor via Gcc-patches wrote:
> +	  if (integer_all_onesp (nelts))
> +	    /* Zero length array.  */
> +	    eltsize = 0;
> +	  else
>  	    {
> -	      tree bnds[] = { TYPE_MIN_VALUE (dom), TYPE_MAX_VALUE (dom) };
> -	      if (TREE_CODE (arg) == COMPONENT_REF)
> -		{
> -		  offset_int size = maxobjsize;
> -		  if (tree fldsize = component_ref_size (arg))
> -		    size = wi::to_offset (fldsize);
> -		  arrbounds[1] = wi::lrshift (size, eltsizelog2);
> -		}
> -	      else if (array_at_struct_end_p (arg) || !bnds[0] || !bnds[1])
> -		arrbounds[1] = wi::lrshift (maxobjsize, eltsizelog2);
> -	      else
> -		arrbounds[1] = (wi::to_offset (bnds[1]) - wi::to_offset (bnds[0])
> -				+ 1) * eltsize;
> +	      tree esz = TYPE_SIZE_UNIT (TREE_TYPE (reftype));
> +	      if (TREE_CODE (esz) == INTEGER_CST)
> +		/* Array element is not a VLA.  */
> +		eltsize = wi::to_offset (esz);
>  	    }
> +
> +	  if (!array_at_struct_end_p (arg)
> +	      && TREE_CODE (nelts) == INTEGER_CST)
> +	    arrbounds[1] = (wi::to_offset (nelts) + 1) * eltsize;
>  	  else
> -	    arrbounds[1] = wi::lrshift (maxobjsize, eltsizelog2);
> +	    {
> +	      /* Use log2 of size to convert the array byte size in to its
> +		 upper bound in elements.  */
> +	      const offset_int eltsizelog2 = wi::floor_log2 (eltsize);
> +	      arrbounds[1] = wi::lrshift (maxobjsize, eltsizelog2);

So, what will this do for zero length arrays at the end of struct?
eltsize will be 0 and wi::floor_log2 (eltsize) is -1, and shifting by -1,
while maybe not UB in wide_int computations, is certainly just weird.
Why do you use eltsize = 0 for the [0] arrays?  They can still have their
element size and if array_at_struct_end_p and the element type is not a variable
length type, using the actual eltsize seems better.
Only when !array_at_struct_end_p we should ensure arrbounds[1] will be 0
and indeed that (wi::to_offset (nelts) + 1) * eltsize would likely not do
that because wi::to_offset is -1ULL or so, not -1.

Also, I'm not sure I understand the right shift by floor_log2 of eltsize,
why can't you simply divide maxobjsize by eltsize (if eltsize is not 0).

	Jakub


  reply	other threads:[~2021-02-17 13:56 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-17  3:34 Martin Sebor
2021-02-17 13:56 ` Jakub Jelinek [this message]
2021-02-17 20:27   ` Martin Sebor
2021-02-17 20:47     ` Jakub Jelinek
2021-02-17 21:11       ` Martin Sebor
2021-02-18  9:30         ` Jakub Jelinek
2021-02-18 16:24           ` Martin Sebor
2021-02-18 18:00             ` Jakub Jelinek
2021-02-18 18:03               ` Jakub Jelinek
2021-02-18 20:55                 ` Martin Sebor
2021-03-09  2:37                   ` Martin Sebor
2021-03-12 13:27                     ` Jakub Jelinek
2021-03-13 21:46                       ` Martin Sebor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210217135636.GN4020736@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=msebor@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).