public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/44386]  New: builtin_object_size_ assumes a flexible array for a long array in a structure of known length
@ 2010-06-02 15:15 meklund at cisco dot com
  2010-06-02 16:33 ` [Bug c/44386] " meklund at cisco dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: meklund at cisco dot com @ 2010-06-02 15:15 UTC (permalink / raw)
  To: gcc-bugs

When the last element of a structure is an array, builtin_object_size__ always
assumes it is a flexible array no matter the length.  For example, the below
code compiled with '-O2' in 4.5.0 gives an unexpected length in 'a', 'b', 'd',
and 'f'.  At a minimum, it is expected that 'a' and 'd' should return 40 since
'e' is returning 40.  It is debatable if 'b' and 'f' should return 40 or the
remaining size of the malloced memory.

#include <stdio.h>

struct bar0 {
    char c[40];
};

struct bar1 {
    char c[40];
    char d[40];
};

struct bar *bp;

int main()
{
    struct bar0 *b0;
    struct bar0 *b0m = malloc(200);
    struct bar1 *b1;
    struct bar1 *b1m = malloc(200);

    printf("%ld\n", __builtin_object_size(b0->c, 3));   // a. Returned 0,
expected 40
    printf("%ld\n", __builtin_object_size(b0m->c, 3));  // b. Returned 200,
expected 40 or 200
    printf("%ld\n", __builtin_object_size(b1->c, 3));   // c. Returned 40,
expected 40
    printf("%ld\n", __builtin_object_size(b1->d, 3));   // d. Returned 0,
expected 40
    printf("%ld\n", __builtin_object_size(b1m->c, 3));  // e. Returned 40,
expected 40
    printf("%ld\n", __builtin_object_size(b1m->d, 3));  // f. Returned 160,
expected 40 or 160
    return 0;
}


-- 
           Summary: builtin_object_size_ assumes a flexible array for a long
                    array in a structure of known length
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: meklund at cisco dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44386


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

end of thread, other threads:[~2010-06-07 15:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-02 15:15 [Bug c/44386] New: builtin_object_size_ assumes a flexible array for a long array in a structure of known length meklund at cisco dot com
2010-06-02 16:33 ` [Bug c/44386] " meklund at cisco dot com
2010-06-02 18:04 ` [Bug middle-end/44386] " pinskia at gcc dot gnu dot org
2010-06-02 18:05 ` [Bug c/44386] " jakub at gcc dot gnu dot org
2010-06-07 15:27 ` [Bug middle-end/44386] " meklund at cisco dot com

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).