mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Szabolcs Nagy <nsz@port70.net>
To: musl@lists.openwall.com
Subject: Re: Documentation of memcpy and undefined behavior in memset
Date: Thu, 6 Jul 2017 18:29:43 +0200	[thread overview]
Message-ID: <20170706162943.GA10974@port70.net> (raw)
In-Reply-To: <0F9B48AD-C5B3-44B6-8D82-0985CF8604A0@trust-in-soft.com>

* Pascal Cuoq <cuoq@trust-in-soft.com> [2017-07-06 14:15:25 +0000]:
> 
> s[0] = s[n-1] = c;
> 
> https://git.musl-libc.org/cgit/musl/tree/src/string/memset.c?id=a08910fc2cc739f631b75b2d09b8d72a0d64d285#n14
> 
> I think this is undefined because i = i++;, which is equivalent to i = i = i + 1;, is the canonical example for the “unsequenced side-effect in expression” undefined behavior(*), and what makes this latter example undefined is the “i = i =” part, not the “i + 1” part. Musl's “s[0] = s[n-1] =” is identical to that when n == 1.
> The same problem occurs in the next lines of memset for other values of n.
> 

i think it's wrong to say that i = i++ is equivalent to i = i = i+1,
since i=i+1 is not the same as i++ (it is almost the same as ++i but
actually subtly different when considering sequencing evaluations so
such analogy is just confusing).

you can say that you think i = i = 0 is ub.

i think the key is that the value computation of an assignment
expression is not sequenced after its side effect (store) (note
that the value computations of the operands are sequenced before it),
so there are two unsequenced stores to the same object, this is not
very obvious from the text of the standard though.


      parent reply	other threads:[~2017-07-06 16:29 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-06 14:15 Pascal Cuoq
2017-07-06 15:52 ` Alexander Monakov
2017-07-06 16:23 ` Rich Felker
2017-07-06 17:02   ` Alexander Monakov
2017-07-06 17:11     ` Rich Felker
2017-07-06 17:17       ` Alexander Monakov
2017-07-06 17:22         ` Rich Felker
2017-07-06 17:38           ` Alexander Monakov
2017-07-06 18:13             ` Rich Felker
2017-07-06 18:52               ` Jens Gustedt
2017-07-06 19:23                 ` Szabolcs Nagy
2017-07-06 23:52                   ` Jens Gustedt
2017-07-06 19:05   ` Bartosz Brachaczek
2017-07-06 19:10     ` Leah Neukirchen
2017-07-06 19:28       ` Szabolcs Nagy
2017-07-06 16:29 ` Szabolcs Nagy [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=20170706162943.GA10974@port70.net \
    --to=nsz@port70.net \
    --cc=musl@lists.openwall.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).