public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jeff Law <law@redhat.com>
To: Martin Sebor <msebor@gmail.com>,
	Richard Biener <richard.guenther@gmail.com>
Cc: gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: Handle strncpy in tree-ssa-dse.c
Date: Mon, 22 Jul 2019 23:26:00 -0000	[thread overview]
Message-ID: <80ac9d5a-f685-d2aa-b919-6baf0dae9684@redhat.com> (raw)
In-Reply-To: <2b0af5d5-e3b0-90be-cf50-a4c4f73e7e8c@gmail.com>

On 7/22/19 9:40 AM, Martin Sebor wrote:
>> Given that they're not allowed to overlap, I would think not.  If that
>> were allowed then the code which aggressively transforms strncpy to
>> memcpy would need to be disabled (or at least throttled back) as well.
> 
> I think there's some (maybe too much) room for interpretation here
> as to exactly what the sentence
> 
>   If copying takes place between objects that overlap, the behavior
>   is undefined.
> 
> means.  Taken to an extreme, one might say that the following
> violates the requirement:
> 
>   char a[4] = "123\0";
>   strcpy (a, a + 2);
> 
> because the call copies bytes within the same object (the array a)
> which inevitably overlaps itself.  But I'm pretty sure that's not
> the intended  interpretation -- the object itself does overlap but
> not the bytes that are copied.  (This is also the view -Wrestrict
> takes.)  If it were undefined, then so would be the following:
> 
>   memcpy (a, a + 2, 2);
> 
> With that in mind, I would be inclined to expect the following not
> to violate the requirement either:
> 
>   strncpy (a, a + 2, 4);
> 
> because the bytes that are copied do not overlap.  With a set to
> "123\0" as done above it's equivalent to:
> 
>   memcpy (a, a + 2, 2);
>   memset (a + 2, 0, 2);
> 
> Admittedly, the examples aren't exactly the same which makes this
> question interesting.  Is it worth raising an interpretation request
> with WG14?
Hmm, I'd tend to go with the more conservative interpretation.  ie,
something like your first example is OK since there is no overlap
between the two regions accessed within the array.

If I confused things by dismissing Richi's example too quickly, then I
apologize.

Can't hurt to get clarification though.

Jeff

      reply	other threads:[~2019-07-22 23:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-19 18:24 Jeff Law
2019-07-22  8:07 ` Richard Biener
2019-07-22 15:11   ` Jeff Law
2019-07-22 15:46     ` Martin Sebor
2019-07-22 23:26       ` Jeff Law [this message]

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=80ac9d5a-f685-d2aa-b919-6baf0dae9684@redhat.com \
    --to=law@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=msebor@gmail.com \
    --cc=richard.guenther@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).