public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* c/9058: structure with flexible array member: offsetof() != sizeof()
@ 2002-12-26  4:36 manfred
  0 siblings, 0 replies; 7+ messages in thread
From: manfred @ 2002-12-26  4:36 UTC (permalink / raw)
  To: gcc-gnats


>Number:         9058
>Category:       c
>Synopsis:       structure with flexible array member: offsetof() != sizeof()
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Thu Dec 26 04:36:01 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     Manfred Spraul
>Release:        gcc (GCC) 3.2 20020903 (RedHat Linux 8.0 3.2-7)
>Organization:
>Environment:
Linux i386
>Description:
See the attached test case:
struct x {int a; char b; short c[];};

Now sizeof(struct x) is 8, offsetof(struct x, c) is 6. That seems to contradice the C99 standard: section 6.7.2.1, constraint 16.

The problem occurs both with gcc and g++
>How-To-Repeat:
gcc -o simple simple.c
./simple
>Fix:
Workarounds:
- Do not assume that x_ptr->c == x_ptr+1
- Manually pad structure
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/octet-stream; name="simple.c"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="simple.c"

LyoKICogVGVzdGNhc2UgZm9yIGEgcG9zc2libGUgY29tcGlsZXIgYnVnOgogKiBzdHJ1Y3R1cmUg
d2l0aCBmbGV4aWJsZSBhcnJheSBtZW1iZXIuCiAqIG9mZnNldG9mKHN0cnVjdCxmbGV4aWJsZV9t
ZW1iZXIpICE9IHNpemVvZihzdHJ1Y3QpLgogKgogKiBUaGF0IHNlZW1zIHRvIGJlIGEgdmlvbGF0
aW9uIG9mIDYuNy4yLjEsIGNvbnN0cmFpbnQgMTYgb2YgdGhlIEM5OQogKiBzdGFuZGFyZDoKICog
IkZpcnN0LCB0aGUgc2l6ZSBvZiB0aGUgc3RydWN0dXJlIHNoYWxsIGJlIGVxdWFsIHRvIHRoZ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W50IG1haW4odm9pZCkK
ewoJcHJpbnRmKCJmaXg6IHNpemVvZjogJWQsIG9mZnNldG9mOiAlZC5cbiIsIHNpemVvZihzdHJ1
Y3QgbjIpLCBvZmZzZXRvZihzdHJ1Y3QgbjIsIGEpKTsKCXByaW50ZigidmFyOiBzaXplb2Y6ICVk
LCBvZmZzZXRvZjogJWQuXG4iLCBzaXplb2Yoc3RydWN0IG4xKSwgb2Zmc2V0b2Yoc3RydWN0IG4x
LCBhKSk7CglyZXR1cm4gMDsKfQo=


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: c/9058: structure with flexible array member: offsetof() != sizeof()
@ 2003-01-13 21:13 bangerth
  0 siblings, 0 replies; 7+ messages in thread
From: bangerth @ 2003-01-13 21:13 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, manfred, nobody

Synopsis: structure with flexible array member: offsetof() != sizeof()

State-Changed-From-To: feedback->closed
State-Changed-By: bangerth
State-Changed-When: Mon Jan 13 13:13:53 2003
State-Changed-Why:
    Closed on request of submitter. Thanks for your quick reply!
    W.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9058


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: c/9058: structure with flexible array member: offsetof() != sizeof()
@ 2003-01-13  0:20 bangerth
  0 siblings, 0 replies; 7+ messages in thread
From: bangerth @ 2003-01-13  0:20 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, manfred, nobody

Synopsis: structure with flexible array member: offsetof() != sizeof()

State-Changed-From-To: open->feedback
State-Changed-By: bangerth
State-Changed-When: Sun Jan 12 16:20:32 2003
State-Changed-Why:
    Manfred,
    what's the status of this report. From the discussion I get
    the impression that the matter has been settled. Is this right?
    Can the report be closed?
    
    Thanks
      W.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=9058


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: c/9058: structure with flexible array member: offsetof() != sizeof()
@ 2002-12-26 15:46 Manfred Spraul
  0 siblings, 0 replies; 7+ messages in thread
From: Manfred Spraul @ 2002-12-26 15:46 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c/9058; it has been noted by GNATS.

From: Manfred Spraul <manfred@colorfullife.com>
To: "Joseph S. Myers" <jsm28@cam.ac.uk>
Cc: gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org
Subject: Re: c/9058: structure with flexible array member: offsetof() != sizeof()
Date: Fri, 27 Dec 2002 00:37:54 +0100

 Joseph S. Myers wrote:
 
 >On Thu, 26 Dec 2002, Manfred Spraul wrote:
 >
 >  
 >
 >>Thanks, I found the thread "C99 conformance bug in gcc-3.1" after filing 
 >>the bug report.
 >>What's the recommended approach to calculate the size for malloc calls? Is
 >>
 >>    struct a {int a; char b; short c[]; };
 >>
 >>    len = offsetof(struct a, c[nr_entries]);
 >>    
 >>
 >
 >You mean offsetof(struct a, c) + nr_entries * sizeof(short).
 >  
 >
 No, I meant what I wrote - is that valid? It would be less error prone, 
 e.g. if the type of the flexible array member is changed.
 linux kernel, i.e gcc portability across platforms.
 
 --
     Manfred
 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: c/9058: structure with flexible array member: offsetof() != sizeof()
@ 2002-12-26 14:26 Joseph S. Myers
  0 siblings, 0 replies; 7+ messages in thread
From: Joseph S. Myers @ 2002-12-26 14:26 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c/9058; it has been noted by GNATS.

From: "Joseph S. Myers" <jsm28@cam.ac.uk>
To: Manfred Spraul <manfred@colorfullife.com>
Cc: <gcc-gnats@gcc.gnu.org>,  <gcc-bugs@gcc.gnu.org>
Subject: Re: c/9058: structure with flexible array member: offsetof() !=
 sizeof()
Date: Thu, 26 Dec 2002 22:24:11 +0000 (GMT)

 On Thu, 26 Dec 2002, Manfred Spraul wrote:
 
 > Thanks, I found the thread "C99 conformance bug in gcc-3.1" after filing 
 > the bug report.
 > What's the recommended approach to calculate the size for malloc calls? Is
 > 
 >     struct a {int a; char b; short c[]; };
 > 
 >     len = offsetof(struct a, c[nr_entries]);
 
 You mean offsetof(struct a, c) + nr_entries * sizeof(short).
 
 > 
 > valid and portable?
 > 
 >     sizeof(struct a)+nr_entries*sizeof(short);
 > 
 > would waste 2 bytes.
 
 Those would be the bytes of padding (that might or might not nominally be
 needed, depending on the size of the array).  I don't believe their
 absence can cause problems unless you directly assign structures for which
 less memory than the size of the original structure has been allocated (in
 which case you'll lose the flexible array members anyway) but you'll need
 to study the detailed wording of the standard (as proposed to be amended)  
 to work out what is guaranteed portable.
 
 -- 
 Joseph S. Myers
 jsm28@cam.ac.uk
 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: c/9058: structure with flexible array member: offsetof() != sizeof()
@ 2002-12-26 13:46 Manfred Spraul
  0 siblings, 0 replies; 7+ messages in thread
From: Manfred Spraul @ 2002-12-26 13:46 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c/9058; it has been noted by GNATS.

From: Manfred Spraul <manfred@colorfullife.com>
To: "Joseph S. Myers" <jsm28@cam.ac.uk>
Cc: gcc-gnats@gcc.gnu.org, gcc-bugs@gcc.gnu.org
Subject: Re: c/9058: structure with flexible array member: offsetof() != sizeof()
Date: Thu, 26 Dec 2002 22:45:04 +0100

 Joseph S. Myers wrote:
 
 >On 26 Dec 2002 manfred@colorfullife.com wrote:
 >
 >  
 >
 >>Now sizeof(struct x) is 8, offsetof(struct x, c) is 6. That seems to
 >>contradice the C99 standard: section 6.7.2.1, constraint 16.
 >>    
 >>
 >
 >Please read the suggested TC in response to DR 282.  (See N983
 ><http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n983.htm> and the Santa Cruz
 >minutes <http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n987.htm> - the DR
 >log hasn't yet been updated.)
 >
 Thanks, I found the thread "C99 conformance bug in gcc-3.1" after filing 
 the bug report.
 What's the recommended approach to calculate the size for malloc calls? Is
 
     struct a {int a; char b; short c[]; };
 
     len = offsetof(struct a, c[nr_entries]);
 
 valid and portable?
 
     sizeof(struct a)+nr_entries*sizeof(short);
 
 would waste 2 bytes.
 
 --
     Manfred
 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: c/9058: structure with flexible array member: offsetof() != sizeof()
@ 2002-12-26 12:26 Joseph S. Myers
  0 siblings, 0 replies; 7+ messages in thread
From: Joseph S. Myers @ 2002-12-26 12:26 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c/9058; it has been noted by GNATS.

From: "Joseph S. Myers" <jsm28@cam.ac.uk>
To: <manfred@colorfullife.com>
Cc: <gcc-gnats@gcc.gnu.org>,  <gcc-bugs@gcc.gnu.org>
Subject: Re: c/9058: structure with flexible array member: offsetof() !=
 sizeof()
Date: Thu, 26 Dec 2002 20:16:24 +0000 (GMT)

 On 26 Dec 2002 manfred@colorfullife.com wrote:
 
 > Now sizeof(struct x) is 8, offsetof(struct x, c) is 6. That seems to
 > contradice the C99 standard: section 6.7.2.1, constraint 16.
 
 Please read the suggested TC in response to DR 282.  (See N983
 <http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n983.htm> and the Santa Cruz
 minutes <http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n987.htm> - the DR
 log hasn't yet been updated.)
 
 -- 
 Joseph S. Myers
 jsm28@cam.ac.uk
 


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2003-01-13 21:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-26  4:36 c/9058: structure with flexible array member: offsetof() != sizeof() manfred
2002-12-26 12:26 Joseph S. Myers
2002-12-26 13:46 Manfred Spraul
2002-12-26 14:26 Joseph S. Myers
2002-12-26 15:46 Manfred Spraul
2003-01-13  0:20 bangerth
2003-01-13 21:13 bangerth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).