That snippet invokes undefined behavior at runtime (violates C++ aliasing 
rules),
so just fix it, rather than bother with bugreports.  E.g. look for 
-fstrict-aliasing
in GCC documentation, or read the C++ standard.  With -fno-strict-aliasing,
which is a workaround for broken code you won't get any warnings about
uninitialized uses.

        Jakub


Yes this code has problems with aliasing. But anyway warning messages are extremely misleading. And I found that that a version which I beleve mustn't have aliasing problems still generates same warnings.

The code:

static uint32_t calc_16bit_checksum_part(uint8_t* buf, int len, uint32_t ret) {
        struct ui16{
                uint16_t d;
        };
        ui16 *ptr = (ui16*)buf;
        for( int i = 0; i < (len / 2); ++i) {
                ret += ptr[i].d;
        }
        if ( len%2) {
                ret += buf[len-1];
        }

        return ret;
}

static uint32_t calc_ch_udp_pseudo(uint32_t src, uint32_t dst, uint16_t len) {
        struct udp_pseudo {
                uint32_t src;
                uint32_t dst;
                uint8_t  z;
                uint8_t  proto;
                uint16_t len;
        } tmp;

        tmp.src = src;
        tmp.dst = dst;
        tmp.z = 0;
        tmp.proto = UDP_PROTO_NUMBER;
        tmp.len = len;

auto ret = calc_16bit_checksum_part((uint8_t*)&tmp, sizeof(tmp), 0);
        return ret;
}

and messages

static uint16_t calc_16bit_checksum(uint8_t* buf, int len) {
                 ^~~~~~~~~~~~~~~~~~~
snippet.cc: In function ‘int main()’:
snippet.cc:66:31: warning: ‘tmp.calc_16bit_checksum_part(uint8_t*, int, uint32_t)::ui16::d’ is used uninitialized in this function [-Wuninitialized]
                 ret += ptr[i].d;
                        ~~~~~~~^
snippet.cc:66:31: warning: ‘*((void*)(& tmp)+2).calc_16bit_checksum_part(uint8_t*, int, uint32_t)::ui16::d’ is used uninitialized in this function [-Wuninitialized] snippet.cc:66:31: warning: ‘*((void*)(& tmp)+4).calc_16bit_checksum_part(uint8_t*, int, uint32_t)::ui16::d’ is used uninitialized in this function [-Wuninitialized] snippet.cc:66:31: warning: ‘*((void*)(& tmp)+6).calc_16bit_checksum_part(uint8_t*, int, uint32_t)::ui16::d’ is used uninitialized in this function [-Wuninitialized] snippet.cc:66:31: warning: ‘*((void*)(& tmp)+8).calc_16bit_checksum_part(uint8_t*, int, uint32_t)::ui16::d’ is used uninitialized in this function [-Wuninitialized] snippet.cc:66:31: warning: ‘*((void*)(& tmp)+10).calc_16bit_checksum_part(uint8_t*, int, uint32_t)::ui16::d’ is used uninitialized in this function [-Wuninitialized]
[


ui16 is an aggregate type and uint8_t is unsigned char, so there will be no undefined behaviour.

Kirill.

Reply via email to