Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Radioactive cats have 18 half-lives.


devel / comp.lang.c / Re: Can we lie to memchr?

SubjectAuthor
* Can we lie to memchr?Kaz Kylheku
+* Re: Can we lie to memchr?Tim Rentsch
|`- Re: Can we lie to memchr?Kaz Kylheku
`- Re: Can we lie to memchr?Ben Bacarisse

1
Can we lie to memchr?

<20230903104255.310@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=28687&group=comp.lang.c#28687

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Can we lie to memchr?
Date: Sun, 3 Sep 2023 17:59:37 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <20230903104255.310@kylheku.com>
Injection-Date: Sun, 3 Sep 2023 17:59:37 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="3db77d17316144525385b79f78a13f40";
logging-data="1084255"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ICDaPebvWUdfTUvC7aiCexRrNVDT4/3s="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:Yj88DCuXhX2Dz1l8UzYV4BV/Ixk=
 by: Kaz Kylheku - Sun, 3 Sep 2023 17:59 UTC

You would think that memchr can be used to test whether a string is
longer than N without traversing it. For instance we can take a
gigabyte-long character string and efficiently test wheether it is
shorter than 10 characters:

memchr(gigastr, 0, 10) == 0

if a null is found within the first 10 bytes, then its length
is 10 or more.

But suppose a 7 byte string is passed (length 6).

That *object* is smaller than n; it does not have an "initial sequence
of n characters" for memchr to search.

ISO C doesn't say that bytes of the initial sequence which are
beyond are sought-after value shall not be accessed by memchr.

For instance, for shits and giggles, memchr could perform a
right-to-left scan, and report the most recently found, hence
leftmost, occurrence of the value.

Or it could assume it can load an 8 byte word from the start of the object
(even if unaligned), since that lies within 10. Yet that 8 could extend
into an unmapped page.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Can we lie to memchr?

<86r0nfqfql.fsf@linuxsc.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=28688&group=comp.lang.c#28688

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Can we lie to memchr?
Date: Sun, 03 Sep 2023 11:22:10 -0700
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <86r0nfqfql.fsf@linuxsc.com>
References: <20230903104255.310@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="6ef63087f855104b64afdd13e03f4862";
logging-data="1088572"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19enkUAjIn5U+J7zBOx2avMZsQK3IAVjEE="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:0ksHIjZDU19hJXdUpAr21hWGj5I=
sha1:Y1LxhvarL4u7WjrVILs6Z9WJ6Ew=
 by: Tim Rentsch - Sun, 3 Sep 2023 18:22 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:

> You would think that memchr can be used to test whether a string is
> longer than N without traversing it. For instance we can take a
> gigabyte-long character string and efficiently test wheether it is
> shorter than 10 characters:
>
> memchr(gigastr, 0, 10) == 0
>
> if a null is found within the first 10 bytes, then its length
> is 10 or more.
>
> But suppose a 7 byte string is passed (length 6).
>
> That *object* is smaller than n; it does not have an "initial sequence
> of n characters" for memchr to search.
>
> ISO C doesn't say that bytes of the initial sequence which are
> beyond are sought-after value shall not be accessed by memchr. [...]

It does, and has for more than 10 years.

Re: Can we lie to memchr?

<87fs3vw1n7.fsf@bsb.me.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=28689&group=comp.lang.c#28689

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: Can we lie to memchr?
Date: Sun, 03 Sep 2023 19:30:04 +0100
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <87fs3vw1n7.fsf@bsb.me.uk>
References: <20230903104255.310@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="4ee5a7923cfe4a48b35c9d5b5ed6532c";
logging-data="1094118"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/cRgiUSb+n9Zuqg3QTU7Jxf7dDUgaWhRU="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:rj/h8FQ4w6iOXaSkR1BYso9rIEM=
sha1:Ffsm5gSzT49anpt9AyT/dP/vzLg=
X-BSB-Auth: 1.7f4fbeb7a12a53dd70f5.20230903193004BST.87fs3vw1n7.fsf@bsb.me.uk
 by: Ben Bacarisse - Sun, 3 Sep 2023 18:30 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:

> You would think that memchr can be used to test whether a string is
> longer than N without traversing it. For instance we can take a
> gigabyte-long character string and efficiently test wheether it is
> shorter than 10 characters:
>
> memchr(gigastr, 0, 10) == 0
>
> if a null is found within the first 10 bytes, then its length
> is 10 or more.
>
> But suppose a 7 byte string is passed (length 6).
>
> That *object* is smaller than n; it does not have an "initial sequence
> of n characters" for memchr to search.
>
> ISO C doesn't say that bytes of the initial sequence which are
> beyond are sought-after value shall not be accessed by memchr.

Well, it does say that

"The implementation shall behave as if it reads the characters sequentially
and stops as soon as a matching character is found."

> For instance, for shits and giggles, memchr could perform a
> right-to-left scan, and report the most recently found, hence
> leftmost, occurrence of the value.
>
> Or it could assume it can load an 8 byte word from the start of the object
> (even if unaligned), since that lies within 10. Yet that 8 could extend
> into an unmapped page.

Only if the behaviour is consistent with the above quote, so anything
going wrong as a result of looking beyond the first occurrence is, I
think, ruled out.

--
Ben.

Re: Can we lie to memchr?

<20230903115634.349@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=28695&group=comp.lang.c#28695

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: Can we lie to memchr?
Date: Sun, 3 Sep 2023 18:58:31 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <20230903115634.349@kylheku.com>
References: <20230903104255.310@kylheku.com> <86r0nfqfql.fsf@linuxsc.com>
Injection-Date: Sun, 3 Sep 2023 18:58:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="3db77d17316144525385b79f78a13f40";
logging-data="1098847"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/JuTGqcfKBnIPzwBIZF0zqZKy0oN32D4c="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:WQ6v81cRPsJo75kJhL/jGzz0UyI=
 by: Kaz Kylheku - Sun, 3 Sep 2023 18:58 UTC

On 2023-09-03, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
> Kaz Kylheku <864-117-4973@kylheku.com> writes:
>> ISO C doesn't say that bytes of the initial sequence which are
>> beyond are sought-after value shall not be accessed by memchr. [...]
>
> It does, and has for more than 10 years.

Thanks, Tim, and also Ben.

I looked in the wrong tab of the PDF reader, where I have C99 open!

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor