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


Groups > comp.lang.c > #110384

Re: Help on this typedef

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Help on this typedef
Date 2017-05-19 09:34 -0700
Organization None to speak of
Message-ID <lno9uo24cj.fsf@kst-u.example.com> (permalink)
References (6 earlier) <7828b0dd-de46-40a2-9636-81117d1f17f6@googlegroups.com> <kfninl2nqqy.fsf@x-alumni2.alumni.caltech.edu> <799899ab-ccf3-49a4-a928-d405dc142fff@googlegroups.com> <lnwp9e15td.fsf@kst-u.example.com> <1c41ec70-5868-4a28-a724-58f4bb70e593@googlegroups.com>

Show all headers | View raw


joel.rees@gmail.com writes:
> On Friday, May 19, 2017 at 1:35:33 AM UTC+9, Keith Thompson wrote:
>> joel.rees@somewhere writes:
>> > On Monday, May 15, 2017 at 11:23:23 PM UTC+9, Tim Rentsch wrote:
>> [...]
>> >> Ah.  In that case I must demur regarding part of your later
>> >> statement.  An access through an array type of extent 1 is
>> >> not definedly allowed to access elements after the element
>> >> at index 0.
>> >
>> > typedef string_header_s struct
>> > {  short length; /* limited length */
>> >    char text[ 1 ];
>> > } string_header_t;
>> 
>> That's called the "struct hack", and it was always of questionable
>> validity.  You can likely get away with allocating extra memory and
>> referring to, for example, obj.text[10], but the behavior is undefined.
>
> You don't believe in reading before you spout, I take it.

I do believe in it, though I don't always succeed.  I have occasionally
made the mistake of posting without reading and understanding an entire
article, and when I do that I appreciate having it pointed out.

> Go back, read the source you clipped and tell me that again.

Ok, I've re-read your previous article and I'll tell you that again.
The behavior is undefined.

Here's a demo program, based on your declaration above with the syntax
error corrected:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
    typedef struct string_header_s {
        short length; /* limited length */
        char text[ 1 ];
    } string_header_t;

    string_header_t *p = malloc(sizeof *p + 10); /* more than we need */
    if (p == NULL) exit(EXIT_FAILURE);
    p->length = 4;
    strcpy(p->text, "abcd");
    printf("p->text[3] = '%c'\n", p->text[3]);
}

When I compile and run this on my system, I get no compile-time
diagnostics and the output, as expected, is:

p->text[3] = 'd'

But both the strcpy() call and the reference to p->text[3] have
undefined behavior.  Concentrating on the latter, the indexing
operator [] is defined in terms of pointer addition, in this case
(p->text + 3).  The description of pointer addition, in N1570
6.5.6p8, says:

    If both the pointer operand and the result point to elements
    of the same array object, or one past the last element of the
    array object, the evaluation shall not produce an overflow;
    otherwise, the behavior is undefined.

The array object in question is p->text, which is of type char[1].
We've used malloc to allocate additional memory past the end of
that array object, but it's not part of the array object.

Most, perhaps all, C compilers will accept this and generate code
that behaves as expected, precisely because the struct hack is
a common idiom.  Nevertheless, the behavior is not defined by
the standard.

If you disagree, I'm paying attention.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Help on this typedef fl <rxjwg98@gmail.com> - 2017-05-10 19:12 -0700
  Re: Help on this typedef Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2017-05-10 21:06 -0600
  Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-10 20:11 -0700
    Re: Help on this typedef David Brown <david.brown@hesbynett.no> - 2017-05-11 10:03 +0200
      Re: Help on this typedef supercat@casperkitty.com - 2017-05-11 08:35 -0700
        Re: Help on this typedef David Brown <david.brown@hesbynett.no> - 2017-05-12 10:34 +0200
          Re: Help on this typedef joel.rees@gmail.com - 2017-05-12 06:01 -0700
          Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-12 09:38 -0700
        Re: Help on this typedef joel.rees@gmail.com - 2017-05-12 05:08 -0700
          Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-13 14:25 -0700
            Re: Help on this typedef joel.rees@gmail.com - 2017-05-14 23:36 -0700
              Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-15 07:23 -0700
                Re: Help on this typedef joel.rees@gmail.com - 2017-05-18 07:40 -0700
                Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-18 09:35 -0700
                Re: Help on this typedef supercat@casperkitty.com - 2017-05-18 10:59 -0700
                Re: Help on this typedef joel.rees@gmail.com - 2017-05-18 23:40 -0700
                Re: Help on this typedef jadill33@gmail.com - 2017-05-19 08:12 -0700
                Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 08:41 -0700
                Re: Help on this typedef jadill33@gmail.com - 2017-05-19 09:41 -0700
                Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-19 09:34 -0700
                Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 10:18 -0700
                Re: Help on this typedef Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-19 21:35 +0100
                Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 14:16 -0700
                Re: Help on this typedef Ike Naar <ike@iceland.freeshell.org> - 2017-05-19 21:15 +0000
                Re: Help on this typedef Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-19 23:54 +0100
                Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-19 14:25 -0700
                Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 15:18 -0700
                Re: Help on this typedef Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-19 23:52 +0100
                Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-19 16:42 -0700
                Re: Help on this typedef Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-20 01:16 +0100
                Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-20 01:58 -0700
                Re: Help on this typedef joel.rees@gmail.com - 2017-05-20 02:25 -0700
                Re: Help on this typedef joel.rees@gmail.com - 2017-05-19 22:09 -0700
                Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 23:23 -0700
                Re: Help on this typedef "James R. Kuyper" <jameskuyper@verizon.net> - 2017-05-19 13:33 -0400
                Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 13:44 -0700
                Re: Help on this typedef jameskuyper@verizon.net - 2017-05-19 19:20 -0700
                Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 23:30 -0700
                Re: Help on this typedef jameskuyper@verizon.net - 2017-05-20 07:20 -0700
                Re: Help on this typedef jameskuyper@verizon.net - 2017-05-20 07:46 -0700
                Re: Help on this typedef supercat@casperkitty.com - 2017-05-22 10:50 -0700
                Re: Help on this typedef "James R. Kuyper" <jameskuyper@verizon.net> - 2017-05-18 12:37 -0400
                Re: Help on this typedef Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-18 20:02 +0100
                Re: Help on this typedef joel.rees@gmail.com - 2017-05-19 00:20 -0700
                Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-19 09:51 -0700
                Re: Help on this typedef supercat@casperkitty.com - 2017-05-19 10:31 -0700
                Re: Help on this typedef Ike Naar <ike@iceland.freeshell.org> - 2017-05-19 19:48 +0000
                Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-20 08:47 -0700
        Re: Help on this typedef "Patrick.Schluter" <Patrick.Schluter@free.fr> - 2017-05-27 18:50 +0200
  Re: Help on this typedef joel.rees@gmail.com - 2017-05-12 06:17 -0700
    Re: Help on this typedef supercat@casperkitty.com - 2017-05-12 08:46 -0700
      Re: Help on this typedef joel.rees@gmail.com - 2017-05-12 18:11 -0700
        Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-12 18:37 -0700
          Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-13 09:44 -0700
            Re: Help on this typedef Keith Thompson <kst-u@mib.org> - 2017-05-13 13:47 -0700
              Re: Help on this typedef Robert Wessel <robertwessel2@yahoo.com> - 2017-05-14 00:28 -0500
              Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-14 13:39 -0700
                Re: Help on this typedef raltbos@xs4all.nl (Richard Bos) - 2017-05-15 13:02 +0000
                Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-15 07:30 -0700
        Re: Help on this typedef supercat@casperkitty.com - 2017-05-12 20:45 -0700
        Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-13 09:57 -0700
        Re: Help on this typedef Philip Lantz <prl@canterey.us> - 2017-05-13 12:07 -0700
        Re: Help on this typedef David Brown <david.brown@hesbynett.no> - 2017-05-14 15:34 +0200
      Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-14 12:48 -0700
        Re: Help on this typedef supercat@casperkitty.com - 2017-05-15 07:39 -0700
          Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-24 08:23 -0700
            Re: Help on this typedef supercat@casperkitty.com - 2017-05-24 09:17 -0700
              Re: Help on this typedef Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-27 06:15 -0700

csiph-web