Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #173802

Can we lie to memchr?

From Kaz Kylheku <864-117-4973@kylheku.com>
Newsgroups comp.lang.c
Subject Can we lie to memchr?
Date 2023-09-03 17:59 +0000
Organization A noiseless patient Spider
Message-ID <20230903104255.310@kylheku.com> (permalink)

Show all headers | View raw


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

Back to comp.lang.c | Previous | NextNext in thread | Find similar


Thread

Can we lie to memchr? Kaz Kylheku <864-117-4973@kylheku.com> - 2023-09-03 17:59 +0000
  Re: Can we lie to memchr? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-09-03 11:22 -0700
    Re: Can we lie to memchr? Kaz Kylheku <864-117-4973@kylheku.com> - 2023-09-03 18:58 +0000
  Re: Can we lie to memchr? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2023-09-03 19:30 +0100

csiph-web