This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: Is ISO memcmp("abc","ade",10000) safe?
Roger Sayle <roger at www dot eyesopen dot com> writes:
|> I'd like to ask the C/C++ language lawyers about their interpretation
|> of the definitions of memcmp given in the relevant ANSI, ISO, POSIX,
|> SVID and BSD specifications.
|>
|> Is memcmp("abc","ade",10000) safe by the standards?
IMHO these are the quotes from the C standard that matter here: the
description of memcmp says:
The memcmp function compares the first n characters of the object
pointed to by s1 to the first n characters of the object pointed to by
s2.
Section 7.1.4 describes valid arguments for such a function (which is an
array of n characters):
If a function argument is described as being an array, the pointer
actually passed to the function shall have a value such that all
address computations and accesses to objects (that would be valid if
the pointer did point to the first element of such an array) are in
fact valid.
This means that _all_ addresses from s1 upto s1+n must be valid (same for
s2), independent of the contents of the array.
Andreas.
--
Andreas Schwab, SuSE Labs, schwab at suse dot de
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."