Menu ▾ ▴

#1163 bad code is accepted

closed-fixed
5
2013-05-25
2006-07-06
No

Hi.

The following (attached) bad code compiles and
produces an obviously bad results:

---
#define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER)
struct x {
unsigned char b;
struct e c[0];
};
unsigned char r(unsigned char n)
{
return offsetof(struct x, c[n]);
}
unsigned char main()
{
return r(5);
}
---

$ sdcc --std-sdcc99 siz.c
[ no error ]

$ gcc siz.c
siz.c:5: error: array type has incomplete element type

Related

Bugs: #4224

Discussion

  • Stas Sergeev

    Stas Sergeev - 2006-07-06

    test-case

     
  • Stas Sergeev

    Stas Sergeev - 2006-08-10
    • labels: 101552 --> 101550
     
  • Stas Sergeev

    Stas Sergeev - 2006-08-10
    • labels: 101550 --> C-Front End
     
  • Maarten Brock

    Maarten Brock - 2006-08-14

    Logged In: YES
    user_id=888171

    Stas,

    Can you please explain what you think is wrong here?

    SDCC generates no error because in C99 it is allowed to
    create a struct ending in an array of zero or unspecified
    length. This is called a "flexible array member". SDCC
    will generate a warning when --std-sdcc89 or --std-c89 is
    used.

    Maarten

     
  • Stas Sergeev

    Stas Sergeev - 2006-08-14

    Logged In: YES
    user_id=501371

    The wrong thing is that the definition of "struct e"
    is missing, but sdcc haven't noticed that.
    You can't compel gcc to accept that code either - that's
    important, I trust gcc. :)

     
  • Thad Smith

    Thad Smith - 2006-09-09

    Logged In: YES
    user_id=965944

    A reference cannot be generated to c[n] of a struct x
    variable if sizeof struct e is unknown, which is true of the
    example.

    C99 allows the last element of a struct to be an "incomplete
    array type", but it cannot be an incomplete array of an
    incomplete structure type.

    The code is a violation of the constraint in section 6.7.5.2
    Array declarators: "The element type shall not be an
    incomplete or function type." and must be diagnosed.

    It is a compiler bug.

     
  • Virgilio Eng. Villatora

    Logged In: YES
    user_id=1610032

    Don't mess around community with these academic (and
    useless) problems.

    Some people use SDCC to manage professional products....

     
  • Stas Sergeev

    Stas Sergeev - 2006-09-30

    Logged In: YES
    user_id=501371

    > Don't mess around community with these academic (and
    > useless) problems.
    > Some people use SDCC to manage professional products....
    If you think that for your "prefessional products" it is
    OK for the compiler to ignore an error and produce the
    wrong code, then I don't think there is something to
    discuss with you.
    FYI, almost every sdcc user use it in an embedded
    production and sometimes even the perople's life may
    depend on the quality of the code.

     
  • Philipp Klaus Krause

    This problem looks similar to #1474589.

    Philipp

     
  • Maarten Brock

    Maarten Brock - 2010-08-05
    • milestone: --> fixed
    • assigned_to: nobody --> maartenbrock
    • status: open --> closed-fixed
     
  • Maarten Brock

    Maarten Brock - 2010-08-05

    Fixed in SDCC 2.9.7 #5915.
    It's not related to #1474589.

     

Log in to post a comment.