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


Groups > comp.lang.c > #160379

"oversized" memchr/wmemchr: UB or not?

From Kaz Kylheku <563-365-8930@kylheku.com>
Newsgroups comp.lang.c
Subject "oversized" memchr/wmemchr: UB or not?
Date 2021-04-26 21:33 +0000
Organization A noiseless patient Spider
Message-ID <20210426141232.677@kylheku.com> (permalink)

Show all headers | View raw


Suppose we have this:

   const *s = "abc";

s[3] is obviously a null byte.

Is this well-defined?

   memchr(s, 0, 5)?

You would think so, because although s does not point to a five byte
object, the function is supposed to find the first null among the bytes.
It will find the null at s[3], which prevents accessing s[4].

But the standard has wording for memchr and wmemchr very similar to
"searches the initial n characters of the object pointed at by s".

There are no "the initial 5 characters of the object pointed at by s"
in our example; the object simply does not have 5 characters.

The requirement is to find the "first occurrence", but there is no
requirement that this be done in a left-to-right search.

It seems an implementation would be conforming if it scanned the
object from right to left, and then reported the most recently seen
pointer, if any, which pointed at the sought-after value.

According to this possibility, the behavior is not well defined if the
size overestimates the actual object.

Come to think of it, we can come up with a similar question for memcmp.

Suppose we have:

   char a[3] = "ab$";
   char b[8] = "abcdefgh";

Does

   memcmp(a, b, 8);

work from left to right and stop at the first mismatch '$' versus 'c'?
Or is it allowed to assume that both objects have at least 6 bytes, and
do the comparison in any order, resulting on out-of-bounds access?

There are programs that would work under the left-to-right
interpretation, if they somehow know that there is always a guaranteed
mismatching character in the shorter object.

Suppose the implementation takes some short-cut, like loading 8 bytes
from each location and compare them as words, to check for an exact
match? a[3] could be just before the end of a page, resulting in a bad
access.


-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Back to comp.lang.c | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

"oversized" memchr/wmemchr: UB or not? Kaz Kylheku <563-365-8930@kylheku.com> - 2021-04-26 21:33 +0000
  Re: "oversized" memchr/wmemchr: UB or not? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-04-26 14:55 -0700
    Re: "oversized" memchr/wmemchr: UB or not? Kaz Kylheku <563-365-8930@kylheku.com> - 2021-04-27 00:31 +0000
      Re: "oversized" memchr/wmemchr: UB or not? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-04-26 21:34 -0700
  Re: "oversized" memchr/wmemchr: UB or not? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-26 20:36 -0700

csiph-web