Hello!
I have a code snippet (actually it is a part of larger project):
#include <stdint.h>
#define UDP_PROTO_NUMBER 17
uint32_t calc_16bit_checksum_part(uint8_t* buf, int len, uint32_t ret) {
uint16_t *ptr = reinterpret_cast<uint16_t*>(buf);
int i;
for( i = 0; i < (len / 2); ++i) {
ret = ret + ptr[i];
}
if ( len%2) {
buf[len] = 0;
ret += ptr[len/2];
}
return ret;
}
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;
};
uint8_t tbuf[sizeof(udp_pseudo)];
udp_pseudo* tmp=reinterpret_cast<udp_pseudo*>(tbuf);
tmp->src = src;
tmp->dst = dst;
tmp->z = 0;
tmp->proto = UDP_PROTO_NUMBER;
tmp->len = len;
return calc_16bit_checksum_part(tbuf, sizeof(tbuf), 0);
}
int main() {
return calc_ch_udp_pseudo(0x01020304, 0x12131415, 320);
}
When I try to compile it with -O2 and higher I got the following
#c++ -DHAVE_CONFIG_H -I. -I.././include -g -O3 -Wall -I../include/
-o snippet snippet.cc
snippet.cc: In function ‘uint32_t calc_ch_udp_pseudo(uint32_t, uint32_t,
uint16_t)’:
snippet.cc:10:34: warning: ‘tbuf’ is used uninitialized in this function
[-Wuninitialized]
ret = ret + ptr[i];
~~~~~^
snippet.cc:10:34: warning: ‘*((void*)& tbuf +2)’ is used uninitialized
in this function [-Wuninitialized]
snippet.cc:10:34: warning: ‘*((void*)& tbuf +4)’ is used uninitialized
in this function [-Wuninitialized]
snippet.cc:10:34: warning: ‘*((void*)& tbuf +6)’ is used uninitialized
in this function [-Wuninitialized]
snippet.cc: In function ‘int main()’:
snippet.cc:10:34: warning: ‘tbuf’ is used uninitialized in this function
[-Wuninitialized]
ret = ret + ptr[i];
~~~~~^
snippet.cc:10:34: warning: ‘*((void*)& tbuf +2)’ is used uninitialized
in this function [-Wuninitialized]
snippet.cc:10:34: warning: ‘*((void*)& tbuf +4)’ is used uninitialized
in this function [-Wuninitialized]
snippet.cc:10:34: warning: ‘*((void*)& tbuf +6)’ is used uninitialized
in this function [-Wuninitialized]
Compiler :
#gcc --versio
gcc (GCC) 7.1.1 20170528
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
My question is. Is this an expected behaviour or I must report a bug ?
Thank you!
Kirill