When unleashing cppcheck on third party sources today of an embedded application, I encountered an issue with the use of unions in this code. The code below is an isolated, simplified example of what is happening.
// cppcheck_union_error.c#include<stdio.h>typedefstruct{unsignedshortmsword_16;unsignedshortlsword_16;}TWO_SHORTS;typedefstruct{union{TWO_SHORTStwo_shorts;unsignedintword_32;}u;}WORDX_TYPE;intmain(void){volatileunsignedint*reg;unsignedintfake_reg;WORDX_TYPEwordx;reg=&fake_reg;wordx.u.two_shorts.msword_16=(1<<5);wordx.u.two_shorts.lsword_16=(1<<4);*reg=wordx.u.word_32;printf("0x%08x\n",*reg);// <== uninitialised var warningwordx.u.two_shorts.msword_16=(1<<3);// <== redundant assignment warningwordx.u.two_shorts.lsword_16=(1<<2);// <== redundant assignment warning*reg=wordx.u.word_32;printf("0x%08x\n",*reg);// <== uninitialised var warningreturn0;}
Program Output:
0x001000200x00040008
Running cppcheck v2.3 on these sources: cppcheck --enable=all cppcheck_union_error.c
This yields a number of incorrect redundantAssignment warnings on the second time the wordx.u.two_shorts members of the union are written. Cppcheck seems to miss that the union members (two shorts and the 32-bit integer) overlap.
Additionally, the printf statements yield an incorrect uninitvar warning.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
When unleashing cppcheck on third party sources today of an embedded application, I encountered an issue with the use of unions in this code. The code below is an isolated, simplified example of what is happening.
Program Output:
Running cppcheck v2.3 on these sources:
cppcheck --enable=all cppcheck_union_error.c
This yields a number of incorrect redundantAssignment warnings on the second time the
wordx.u.two_shorts
members of the union are written. Cppcheck seems to miss that the union members (two shorts and the 32-bit integer) overlap.Additionally, the printf statements yield an incorrect uninitvar warning.