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


Groups > comp.lang.c > #160393

Re: "oversized" memchr/wmemchr: UB or not?

From Andrey Tarasevich <andreytarasevich@hotmail.com>
Newsgroups comp.lang.c
Subject Re: "oversized" memchr/wmemchr: UB or not?
Date 2021-04-26 20:36 -0700
Organization A noiseless patient Spider
Message-ID <s680st$6v9$1@dont-email.me> (permalink)
References <20210426141232.677@kylheku.com>

Show all headers | View raw


On 4/26/2021 2:33 PM, Kaz Kylheku wrote:
 >
 > 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?
 >

A question that has been raised quote a few times before, since it is 
connected to obvious optimization opportunities. This 2015 entry by 
Pascal Cuoq

https://trust-in-soft.com/blog/2015/12/21/memcmp-requires-pointers-to-fully-valid-buffers/

points out the difference in wordings for `memchr` and `memcmp`. The 
former explicitly says that "the implementation shall behave as if it 
reads the characters sequentially and stops as soon as a matching 
character is found."

There seems to be no such requirement for `memcmp`. According to the 
link, Glibc implementation of `memcmp` will indeed overstep the 
boundaries of the buffer in both optimization branches: common alignment 
and no common alignment. In the latter case it might overstep by a 
rather large margin, and if circumstances are right it can be shown to 
cause a crash.

-- 
Best regards,
Andrey Tarasevich

Back to comp.lang.c | Previous | Next — Previous 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