Groups | Search | Server Info | Login | Register
| Path | csiph.com!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Ben Bacarisse <ben@bsb.me.uk> |
| Newsgroups | comp.std.c |
| Subject | Re: May a string span multiple, independent objects? |
| Date | Wed, 03 Jul 2024 22:08:39 +0100 |
| Organization | A noiseless patient Spider |
| Lines | 55 |
| Message-ID | <87zfqy6v54.fsf@bsb.me.uk> (permalink) |
| References | <20240703141500$00ed@vinc17.org> <v63sjf$28fl8$3@dont-email.me> |
| MIME-Version | 1.0 |
| Content-Type | text/plain |
| Injection-Date | Wed, 03 Jul 2024 23:08:40 +0200 (CEST) |
| Injection-Info | dont-email.me; posting-host="91547590b01ff16af44327f627e0520c"; logging-data="2484351"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18sRU6gFAM/+L25cI31b+OWUz/25oTiJ8E=" |
| User-Agent | Gnus/5.13 (Gnus v5.13) |
| Cancel-Lock | sha1:AjUqDI+ctz9tXZVCh1WdOnZcKmo= sha1:qW7w+yGCAv5p9kZOvjhpIS7O5o8= |
| X-BSB-Auth | 1.0fb6437067440fbff3c2.20240703220839BST.87zfqy6v54.fsf@bsb.me.uk |
| Xref | csiph.com comp.std.c:6651 |
Show key headers only | View raw
James Kuyper <jameskuyper@alumni.caltech.edu> writes:
> On 7/3/24 10:31, Vincent Lefevre wrote:
>> ISO C17 (and C23 draft) 7.1.1 defines a string as follows: "A string
>> is a contiguous sequence of characters terminated by and including
>> the first null character."
>>
>> But may a string span multiple, independent objects that happens
>> to be contiguous in memory?
...
>> For instance, is the following program valid and what does the ISO C
>> standard say about that?
>>
>> #include <stdio.h>
>> #include <string.h>
>>
>> typedef char *volatile vp;
>>
>> int main (void)
>> {
>> char a = '\0', b = '\0';
>
> a and b are not guaranteed to be contiguous.
>
>> vp p = &a, q = &b;
>>
>> printf ("%p\n", (void *) p);
>> printf ("%p\n", (void *) q);
>> if (p + 1 == q)
>> {
>
> That comparison is legal, and has well-defined behavior. It will be true
> only if they are in fact contiguous.
>
>> a = 'x';
>> printf ("%zd\n", strlen (p));
>
> Because strlen() must take a pointer to 'a' (which is treated, for these
> purposes, as a array of char of length 1), and increment it one past the
> end of that array, and then dereference that pointer to check whether it
> points as a null character, the behavior is undefined.
I think this is slightly misleading. It suggests that the UB comes from
something strlen /must/ do, but strlen must be thought of as a black
box. We can't base anyhting on a assumed implementation.
But our conclusion is correct because there is explicit wording covering
this case. The section on "String function conventions" (7.24.1)
states:
"If an array is accessed beyond the end of an object, the behavior is
undefined."
--
Ben.
Back to comp.std.c | Previous | Next — Previous in thread | Next in thread | Find similar
May a string span multiple, independent objects? Vincent Lefevre <vincent-news@vinc17.net> - 2024-07-03 14:31 +0000
Re: May a string span multiple, independent objects? Hans-Bernhard Bröker <HBBroeker@gmail.com> - 2024-07-03 17:23 +0200
Re: May a string span multiple, independent objects? Vincent Lefevre <vincent-news@vinc17.net> - 2024-07-03 15:37 +0000
Re: May a string span multiple, independent objects? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-07-03 12:11 -0400
Re: May a string span multiple, independent objects? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-08 08:51 -0700
Re: May a string span multiple, independent objects? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-07-03 11:59 -0400
Re: May a string span multiple, independent objects? Ben Bacarisse <ben@bsb.me.uk> - 2024-07-03 22:08 +0100
Re: May a string span multiple, independent objects? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-07-03 17:36 -0400
Re: May a string span multiple, independent objects? Vincent Lefevre <vincent-news@vinc17.net> - 2024-07-04 13:22 +0000
Re: May a string span multiple, independent objects? Ben Bacarisse <ben@bsb.me.uk> - 2024-07-05 05:14 +0100
Re: May a string span multiple, independent objects? James Kuyper <jameskuyper@alumni.caltech.edu> - 2024-07-05 01:37 -0400
Re: May a string span multiple, independent objects? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-08-08 08:35 -0700
Re: May a string span multiple, independent objects? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-07-05 07:14 +0000
csiph-web