* [manual]: rawmemchr(3) and UB
@ 2022-12-29 19:19 Alejandro Colomar
2022-12-29 19:27 ` Alejandro Colomar
2022-12-29 19:45 ` Cristian Rodríguez
0 siblings, 2 replies; 5+ messages in thread
From: Alejandro Colomar @ 2022-12-29 19:19 UTC (permalink / raw)
To: GNU C Library; +Cc: linux-man
[-- Attachment #1.1: Type: text/plain, Size: 1414 bytes --]
Hi,
I was reading rawmemchr(3), and found some funny text:
RETURN VALUE
The memchr() and memrchr() functions return a pointer to the matching
byte or NULL if the character does not occur in the given memory area.
The rawmemchr() function returns a pointer to the matching byte, if one
is found. If no matching byte is found, the result is unspecified.
Of course, if the byte is not found, the result is not unspecified, but rather
undefined, and a crash is very likely so maybe there's not even a result. I
thought this might be a thinko of the manual page, but the glibc manual seems to
have similar text:
<https://www.gnu.org/software/libc/manual/html_mono/libc.html#index-rawmemchr>
"
The rawmemchr function exists for just this situation which is surprisingly
frequent. The interface is similar to memchr except that the size parameter is
missing. The function will look beyond the end of the block pointed to by block
in case the programmer made an error in assuming that the byte c is present in
the block. In this case the result is unspecified. Otherwise the return value is
a pointer to the located byte.
"
That test can't be true, and the result of that function when there's no match
can't be anything other than UB, and likely a crash. Please fix the doc.
Cheers,
Alex
--
<http://www.alejandro-colomar.es/>
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [manual]: rawmemchr(3) and UB
2022-12-29 19:19 [manual]: rawmemchr(3) and UB Alejandro Colomar
@ 2022-12-29 19:27 ` Alejandro Colomar
2022-12-29 19:45 ` Cristian Rodríguez
1 sibling, 0 replies; 5+ messages in thread
From: Alejandro Colomar @ 2022-12-29 19:27 UTC (permalink / raw)
To: GNU C Library; +Cc: linux-man
[-- Attachment #1.1: Type: text/plain, Size: 1597 bytes --]
On 12/29/22 20:19, Alejandro Colomar via Libc-alpha wrote:
> Hi,
>
> I was reading rawmemchr(3), and found some funny text:
>
> RETURN VALUE
> The memchr() and memrchr() functions return a pointer to the matching
> byte or NULL if the character does not occur in the given memory area.
>
> The rawmemchr() function returns a pointer to the matching byte, if one
> is found. If no matching byte is found, the result is unspecified.
>
>
> Of course, if the byte is not found, the result is not unspecified, but rather
> undefined, and a crash is very likely so maybe there's not even a result. I
> thought this might be a thinko of the manual page, but the glibc manual seems to
> have similar text:
>
>
> <https://www.gnu.org/software/libc/manual/html_mono/libc.html#index-rawmemchr>
> "
> The rawmemchr function exists for just this situation which is surprisingly
> frequent. The interface is similar to memchr except that the size parameter is
> missing. The function will look beyond the end of the block pointed to by block
> in case the programmer made an error in assuming that the byte c is present in
> the block. In this case the result is unspecified. Otherwise the return value is
> a pointer to the located byte.
> "
>
>
> That test can't be true, and the result of that function when there's no match
s/test/text/
> can't be anything other than UB, and likely a crash. Please fix the doc.
>
> Cheers,
>
> Alex
--
<http://www.alejandro-colomar.es/>
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [manual]: rawmemchr(3) and UB
2022-12-29 19:19 [manual]: rawmemchr(3) and UB Alejandro Colomar
2022-12-29 19:27 ` Alejandro Colomar
@ 2022-12-29 19:45 ` Cristian Rodríguez
2022-12-29 19:50 ` Alejandro Colomar
1 sibling, 1 reply; 5+ messages in thread
From: Cristian Rodríguez @ 2022-12-29 19:45 UTC (permalink / raw)
To: Alejandro Colomar; +Cc: GNU C Library, linux-man
On Thu, Dec 29, 2022 at 4:20 PM Alejandro Colomar via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> Hi,
>
> I was reading rawmemchr(3), and found some funny text:
>
> RETURN VALUE
> The memchr() and memrchr() functions return a pointer to the matching
> byte or NULL if the character does not occur in the given memory area.
>
> The rawmemchr() function returns a pointer to the matching byte, if one
> is found. If no matching byte is found, the result is unspecified.
>
>
> Of course, if the byte is not found, the result is not unspecified, but rather
> undefined, and a crash is very likely so maybe there's not even a result. I
> thought this might be a thinko of the manual page, but the glibc manual seems to
> have similar text:
>
The library itself uses this function mostly to find NULL as an
optimization. This is all before GCC handled all of this so it is
mostly obsolete.
gcc replaces null byte searches that use str*chr with s + strlen(s)
and expands memchr c=null and rawmemchr-like patterns inline.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [manual]: rawmemchr(3) and UB
2022-12-29 19:45 ` Cristian Rodríguez
@ 2022-12-29 19:50 ` Alejandro Colomar
2022-12-30 10:31 ` Alejandro Colomar
0 siblings, 1 reply; 5+ messages in thread
From: Alejandro Colomar @ 2022-12-29 19:50 UTC (permalink / raw)
To: Cristian Rodríguez; +Cc: GNU C Library, linux-man
[-- Attachment #1.1: Type: text/plain, Size: 1605 bytes --]
Hi Cristian,
On 12/29/22 20:45, Cristian Rodríguez wrote:
> On Thu, Dec 29, 2022 at 4:20 PM Alejandro Colomar via Libc-alpha
> <libc-alpha@sourceware.org> wrote:
>>
>> Hi,
>>
>> I was reading rawmemchr(3), and found some funny text:
>>
>> RETURN VALUE
>> The memchr() and memrchr() functions return a pointer to the matching
>> byte or NULL if the character does not occur in the given memory area.
>>
>> The rawmemchr() function returns a pointer to the matching byte, if one
>> is found. If no matching byte is found, the result is unspecified.
>>
>>
>> Of course, if the byte is not found, the result is not unspecified, but rather
>> undefined, and a crash is very likely so maybe there's not even a result. I
>> thought this might be a thinko of the manual page, but the glibc manual seems to
>> have similar text:
>>
>
> The library itself uses this function mostly to find NULL as an
> optimization. This is all before GCC handled all of this so it is
> mostly obsolete.
> gcc replaces null byte searches that use str*chr with s + strlen(s)
> and expands memchr c=null and rawmemchr-like patterns inline.
You mean that GCC does the following?:
inline size_t
strlen(const char *s)
{
return rawmemchr(s, '\0');
}
If so, great, because I am writing a libc replacement, and was implementing
strlen(3) exactly like that, which is why I needed the docs. It may be
something not very useful, but I guess it's still very useful for libc internals.
Cheers,
Alex
--
<http://www.alejandro-colomar.es/>
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [manual]: rawmemchr(3) and UB
2022-12-29 19:50 ` Alejandro Colomar
@ 2022-12-30 10:31 ` Alejandro Colomar
0 siblings, 0 replies; 5+ messages in thread
From: Alejandro Colomar @ 2022-12-30 10:31 UTC (permalink / raw)
To: Cristian Rodríguez; +Cc: GNU C Library, linux-man
[-- Attachment #1.1: Type: text/plain, Size: 1369 bytes --]
Hi Cristian,
On 12/29/22 20:50, Alejandro Colomar wrote:
> On 12/29/22 20:45, Cristian Rodríguez wrote:
>> On Thu, Dec 29, 2022 at 4:20 PM Alejandro Colomar via Libc-alpha
>> <libc-alpha@sourceware.org> wrote:
[...]
>>
>> The library itself uses this function mostly to find NULL as an
>> optimization. This is all before GCC handled all of this so it is
>> mostly obsolete.
>> gcc replaces null byte searches that use str*chr with s + strlen(s)
>> and expands memchr c=null and rawmemchr-like patterns inline.
>
> You mean that GCC does the following?:
>
>
> inline size_t
> strlen(const char *s)
> {
> return rawmemchr(s, '\0');
Obvious typo here: I forgot to subtract s.
> }
>
>
> If so, great, because I am writing a libc replacement, and was implementing
> strlen(3) exactly like that, which is why I needed the docs. It may be
> something not very useful, but I guess it's still very useful for libc internals.
>
It seems I misunderstood your email. I've seen that glibc implements
rawmemchr(3) in terms of strlen(3) and memchr(3). So it seems better to just
not implement this function in my library, and optimize strlen(3) directly. The
non-'\0' case seems useless, so probably not worth this function unless I see a
use for it.
Cheers,
Alex
--
<http://www.alejandro-colomar.es/>
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2022-12-30 10:31 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-29 19:19 [manual]: rawmemchr(3) and UB Alejandro Colomar
2022-12-29 19:27 ` Alejandro Colomar
2022-12-29 19:45 ` Cristian Rodríguez
2022-12-29 19:50 ` Alejandro Colomar
2022-12-30 10:31 ` Alejandro Colomar
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox