"Uninitialized array" warnings by c++ with -O2
Kirill Yu Berezin
kyb22@rol.ru
Wed Jun 7 09:16:00 GMT 2017
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
More information about the Gcc
mailing list