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

* [Bug c/44386] builtin_object_size_ assumes a flexible array for a long array in a structure of known length
  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 ` meklund at cisco dot com
  2010-06-02 18:04 ` [Bug middle-end/44386] " pinskia at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: meklund at cisco dot com @ 2010-06-02 16:33 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from meklund at cisco dot com  2010-06-02 16:33 -------
I've re-run the code varying the flag from 0 to 3 (instead of hard coding to 3)
with the below results.  The unexpected results are still happening for 'a',
'b', 'd', and 'f'.

    a   b  c  d   e   f
0: -1 200 -1 -1 200 160
1: -1 200 40 -1  40 160
2:  0 200  0  0 200 160
3:  0 200 40  0  40 160


-- 


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


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

* [Bug middle-end/44386] builtin_object_size_ assumes a flexible array for a long array in a structure of known length
  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 ` 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
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2010-06-02 18:04 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pinskia at gcc dot gnu dot org  2010-06-02 18:04 -------
I think this is by design because in C90 and C++98 there is no way to say an
array is a flexible array.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c                           |middle-end


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


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

* [Bug c/44386] builtin_object_size_ assumes a flexible array for a long array in a structure of known length
  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 ` jakub at gcc dot gnu dot org
  2010-06-07 15:27 ` [Bug middle-end/44386] " meklund at cisco dot com
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu dot org @ 2010-06-02 18:05 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from jakub at gcc dot gnu dot org  2010-06-02 18:05 -------
This is intentional, given the amount of code in the wild that uses various
fixed length arrays in last fields.


-- 

jakub at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
          Component|middle-end                  |c
         Resolution|                            |INVALID


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


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

* [Bug middle-end/44386] builtin_object_size_ assumes a flexible array for a long array in a structure of known length
  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
                   ` (2 preceding siblings ...)
  2010-06-02 18:05 ` [Bug c/44386] " jakub at gcc dot gnu dot org
@ 2010-06-07 15:27 ` meklund at cisco dot com
  3 siblings, 0 replies; 5+ messages in thread
From: meklund at cisco dot com @ 2010-06-07 15:27 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from meklund at cisco dot com  2010-06-07 15:26 -------
I see your point that some legacy code might use a larger size as a flexible
array.

What is you opinion on the possibility of adding a bit-flag to
__builtin_object_size() (like 0x04) that tightens the allowed flexible array
size to be only 0 or 1?  Larger sizes would be accepted as the total array
size.  This would be closer to that in
http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Zero-Length.html#Zero-Length.


-- 


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