Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #160382
| Path | csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
| Newsgroups | comp.lang.c |
| Subject | Re: "oversized" memchr/wmemchr: UB or not? |
| Date | Mon, 26 Apr 2021 14:55:46 -0700 |
| Organization | None to speak of |
| Lines | 82 |
| Message-ID | <87im48bti5.fsf@nosuchdomain.example.com> (permalink) |
| References | <20210426141232.677@kylheku.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain |
| Injection-Info | reader02.eternal-september.org; posting-host="2f1060674337b43b72c53c3cc660a10f"; logging-data="16502"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX194WTRK0Ao75BZvpM7cJT2K" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) |
| Cancel-Lock | sha1:X03wpjrAbVR2wpJ3QnMpjWemUds= sha1:Z4k9ozTeBlAWc560Flx00jZEF1Y= |
| Xref | csiph.com comp.lang.c:160382 |
Show key headers only | View raw
Kaz Kylheku <563-365-8930@kylheku.com> writes:
> 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.
N1570 7.24.5.1:
The memchr function locates the first occurrence of c (converted to
an unsigned char) in the initial n characters (each interpreted as
unsigned char) of the object pointed to by s.
This might imply that if there are no "initial n characters", the
behavior is undefined. However, this is followed by:
The implementation shall behave as if it reads the characters
sequentially and stops as soon as a matching character is found.
That's more explicit, and I think the intent is that your call is
well defined. It could be stated more clearly, though. If the first
sentence is taken to imply that your call has undefined behavior,
the "shall behave as if" in the second sentence doesn't necessarily
override that.
> 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?
The description of memcmp says it "compares the first n characters", and
has no "as if" wording. I'd say that if examining the first n
characters of both objects would have undefined behavior, then memcmp
would have undefined behavior.
> 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.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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