Groups | Search | Server Info | Login | Register
| From | Kaz Kylheku <643-408-1753@kylheku.com> |
|---|---|
| Newsgroups | comp.std.c |
| Subject | Re: May a string span multiple, independent objects? |
| Date | 2024-07-05 07:14 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <20240705000419.170@kylheku.com> (permalink) |
| References | <20240703141500$00ed@vinc17.org> |
On 2024-07-03, Vincent Lefevre <vincent-news@vinc17.net> 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?
It is undefined behavior. Implementations are allowed to track the
provenance of a displaced pointer, and diagnose when it is out of bounds
even if the displaced value points into a valid object, and even if th
eprogram validates that via a well-defined equality test.
> 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';
> vp p = &a, q = &b;
>
> printf ("%p\n", (void *) p);
> printf ("%p\n", (void *) q);
> if (p + 1 == q)
> {
> a = 'x';
> printf ("%zd\n", strlen (p));
> }
In this situation, the p + 1 expression is well-defined as well
the p + 1 == q test.
However, while *q is a valid expression that evaluates to zero,
*(p + 1) isn't valid. The one byte past the object pointer value
may not be dereferenced.
The equivalence p + 1 == q doesn't save it; p + 1 is displaced from p,
unrelated to q.
> if (q + 1 == p)
> {
> b = 'x';
> printf ("%zd\n", strlen (q));
> }
> return 0;
> }
>
> If such a program is valid, would there be issues by working with
> pointers on such a string, say, dereferencing p[1] in the first "if"
> (which is normally UB)?
An issue could be that the implementation's optimizer assumes that
p + 1 and q are poiners to distinct objects, even in the middle
of a block of code that is conditional on p + 1 == q.
If the code executes *(p + 1) = 'a', a subsequent evaluation of
*q or b can still produce 0.
--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
Back to comp.std.c | Previous | Next — Previous 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