Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#1446 broken offsetof() handling in SDCCast.c

closed-fixed
Maarten Brock
5
2013-05-25
2008-04-02
Robert Larice
No

struct st {
char a;
};

char * buggy(){
return &(((struct st code *) 0x1234) -> a);
}

returns an incorrect generic pointer at least for
mcs51 target.

the problem is caused by a special handling of
cast's resembling the above one,
in file SDCCast.c:3961

this special handling was built-in to process
the the following macro (comment from SDCCast.c)

/* handle offsetof macro: */
/* #define offsetof(TYPE, MEMBER) \ */
/* ((unsigned) &((TYPE *)0)->MEMBER) */

which in my opinion is naive anyway.
offsets are better calculated by substraction of pointers, something very remotly like:
#define offsetof(TYPE, MEMBER) \ (((int) &((TYPE*)0)->MEMBER) - (int) &(TYPE*)0)

i've attached a testcase for support/regression...
(tested only for mcs51-small)
the testcase successfully destinguishes sdcc
from a modified sdcc with disabled SDCCast.c:3961

Robert Larice

Discussion

  • Robert Larice
    Robert Larice
    2008-04-02

    testcase for support/regression/tests

     
    Attachments
  • Patryk
    Patryk
    2008-04-03

    Logged In: YES
    user_id=1788180
    Originator: NO

    Obviously SDCC got problems with such casts:
    [ 1928022 ] ROM pointer to struct member not initialized
    [ 1921073 ] Bad code when casting from code to generic ptr in 2.8.0 RC1 (surfaced and killed just before 2.8.0 release)
    [ 1839277 ] Implicit typecast from code to generic pointer is wrong
    etc.

    BTW: offsetof(type, member) macro is defined like ((size_t) &(((type *) 0)->member)) in every of 4 compilers (embedded and desktop) I'm currently using.

     
  • Maarten Brock
    Maarten Brock
    2010-08-05

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

    This appears to have been fixed before SDCC 2.9.7 #5915.