Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #160387
| From | Kaz Kylheku <563-365-8930@kylheku.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: "oversized" memchr/wmemchr: UB or not? |
| Date | 2021-04-27 00:31 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <20210426164144.416@kylheku.com> (permalink) |
| References | <20210426141232.677@kylheku.com> <87im48bti5.fsf@nosuchdomain.example.com> |
On 2021-04-26, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote: > 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. Very interesting; this was not found in C99. Therefore a C99 implementation of the library need not implement any interpretation of this requirement. > > 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. What we want is something "the implementation shall not access bytes which follow the first matching byte". Well, we don't really want that, because that wording goes too far. Any byte of the object that is okay to access is contained in a larger word that is also safe to access, provided it is aligned, and that is part of a useful optimization which works fine in the case of interest. The phrase "as if" is not formally defined. Most uses of it in the document are used to for giving requirements about a programming language construct by indicating that it must behave "as if" it were some imaginary augmented or alternative construct. Those uses of "as if" no way require the implementation to work by actual trasformation to the alternative construct. Scanning the object from the right and coming up with the last occurrence of the character is still "as if" it were being done left to right to stop at the first occurrence. By the way, interestingly, though we have this informal concept named "as if rule" which goes way back, no C standard (that I can get my hands on between C89 and N1570) uses "as if" in the text that gives the relationship between abstract and actual semantics. In other words, the "as if rule" does not come from any literal quote in a C standard. The "as if" of "as if rule" does not help here, in any case. -- TXR Programming Language: http://nongnu.org/txr Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
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