> that phrase with anything to conclude this. ...
True, but you do need to combine it with something else to conclude that
the absence of an object is a problem. There's no constraint saying that
it must point at an object. It's certainly not a syntax error. There's
no explicit statement that the behavior is undefined. If the count were
non-zero, and the pointer did not point at an object, the behavior would
be implicitly undefined "... by the
omission of any explicit definition of behavior", since the only
explicit description that would otherwise be applicable becomes
meaningless if there's no object in the pointed-at location. However,
when the count is zero, the defined behavior is very explicitly a no-op,
which makes just as much sense whether there is or is not an object in
the location pointed at.
> ... We need something which
> overrides the default meaning. 7.1.4p1's wording doesn't, since it
> doesn't cover this case - though it may intend to.
> If your memcpy reads nothing from s2 when copying 0 bytes, that could be
> considered an implementation detail unless something says it must read
> nothing, as in "the argument must be treated as volatile const void*".
> Otherwise memcpy() could read the first byte (or more likely the word
> containing that byte) before checking the length, ...
Under the as-if rule, the compiler is always allowed to insert a
spurious read from memory anywhere it likes, so long as it doesn't have
any consequences that matter, such as violating protected memory.
However, such a read would be fully spurious in this context - the only
thing that gives memcpy() permission to read any memory is the
description that gives it permission to copy from that memory; and
7.1.4p1 explicitly specifies that when the count is zero, memcpy() has
no permission to copy; therefore, it has no permission to read. That
seems an obvious implication from the main description of memcpy(), but
without 7.1.4p1, it could have been concluded from the description that
a count of 0 was an invalid argument value.
If you think that memcpy() does have permission to read from that
memory, even in contexts where such a read would be problematic, and
could therefore not be justified by the as-if rule, please explain why.
--
James Kuyper