Should memchr work with invalid data size?

Adhemerval Zanella adhemerval.zanella@linaro.org
Thu May 18 14:12:00 GMT 2017



On 18/05/2017 04:42, Andreas Schwab wrote:
> On Mai 18 2017, Florian Weimer <fw@deneb.enyo.de> wrote:
> 
>> POSIX is more clear:
>>
>> | Implementations shall behave as if they read the memory byte by byte
>> | from the beginning of the bytes pointed to by s and stop at the
>> | first occurrence of c (if it is found in the initial n bytes).
>>
>> This language is not found in C99.
> 
> In C11 it has been amended and says basically the same:
> 
> "The implementation shall behave as if it reads the characters
> sequentially and stops as soon as a matching character is found."
> 
> Andreas.
> 

It was also discussed on BZ#12019 and lead to other bug reports to
actually fixe it on various implementations (BZ#21182, commit 
502697713f4f1, BZ#19387, BZ#20971, BZ#21014, BZ#11230).

As per comment #9 memchr constraints was changed [1] to:

"A number of changes were agreed as follows

Change p1284 lines 42163-42164
In the DESCRIPTION remove "of the object" from

The memchr( ) function shall locate the first occurrence of c (converted
to an unsigned char) in the initial n bytes (each interpreted as unsigned
char) of the object pointed to by s.

In the RETURN VALUE section

The memchr( ) function shall return a pointer to the located byte,
or a null pointer if the byte does not occur in the object.

to
The memchr( ) function shall return a pointer to the located byte,
or a null pointer if the byte is not found.

Add to DESCRIPTION
Implementations shall behave as if they read the memory byte by byte
from the beginning of the bytes pointed to by s and stop at the first
occurrence of c."

So calling memchr with all possible values of size_t is a valid construction.

[1] http://www.opengroup.org/austin/docs/austin_454.txt



More information about the Libc-alpha mailing list