Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#2242 pointer acrobatics in sfcUtil/genericlist.c

Performance
open
nobody
5
2011-07-05
2011-07-04
Klaus Kämpf
No

Compiling sfccommon leads to lots of compiler warnings when compiling sfcUtil/genericlist.c
i.e.
sfcUtil/genericlist.c: In function 'listRelease':
sfcUtil/genericlist.c:556:3: warning: dereferencing type-punned pointer will break strict-aliasing rules

Patch follows ...

Discussion

  • Chris Buccella
    Chris Buccella
    2011-07-04

    > Patch follows ...

    Did you forget to attach it?

    Also, I'm not seeing this warning when I compile. What gcc version are you using? I have 4.4.4 here.

     
  • Klaus Kämpf
    Klaus Kämpf
    2011-07-05

    Ah, sorry, the patch is not ready yet.

    Compiler is gcc 4.5.1 with "-Wall"

     
  • Klaus Kämpf
    Klaus Kämpf
    2011-07-05

    Adapting summary to reflect root cause of the problem.

    e.g. newList() does
    UtilList ul;
    initialize_list((Generic_list *) & ul.hdl);
    and then initialize_list() continues with
    list->info = ...

    - the call to initialize_list casts a **void to Generic_list*
    - the info element of Generic_list happens to be the first element (offset 0), just list hdr of UtilList
    - thus by chance list->info dereferences to ul.hdr
    - there's never a real pointer to a Generic_list, only to Generic_list.info. Luckily the static list manipulation functions only access list.info

     
  • Klaus Kämpf
    Klaus Kämpf
    2011-07-05

    • summary: sblim-sfcCommon warnings: dereferencing type-punned pointer --> pointer acrobatics in sfcUtil/genericlist.c
     
  • Klaus Kämpf
    Klaus Kämpf
    2011-07-05

    Possible patch

     
  • Klaus Kämpf
    Klaus Kämpf
    2011-07-05

    Possible patch attached. Still ugly as it depends on ul.hdr and list.info being on offset 0.
    Another fix is to set "-fno-strict-aliasing" in CFLAGS, but this would break optimization.

    genericlist.c is up for a larger rewrite I guess ;-)