<div dir="ltr">I'm trying to understand an issue reported by Clang's static analysis tool.  The code below demonstrates the issue:<div><span style="font-family:monospace"><span style="font-weight:bold;color:rgb(84,255,84)"><br></span></span></div><div><font face="monospace, monospace">$</font><span style="font-family:monospace"><span style="font-weight:bold;color:rgb(84,255,84)"> </span><span style="color:rgb(0,0,0)">cat problem.c </span></span></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">#include <stdint.h>
</span><br>
<br>int main() {
<br>#if VARIANT==1
<br>    uint32_t data = 0xdeadbeef;
<br>    uint8_t* byte = (uint8_t*)&data;
<br>    uint8_t value = byte[0];
<br>#elif VARIANT==2
<br>    uint32_t data = 0xdeadbeef;
<br>    uint8_t* byte = (uint8_t*)&data;
<br>    uint8_t value = byte[1];
<br>#elif VARIANT==3
<br>    uint32_t data[1] = {0xdeadbeef};
<br>    uint8_t* byte = (uint8_t*)&data[0];
<br>    uint8_t value = byte[0];
<br>#elif VARIANT==4
<br>    uint32_t data[1] = {0xdeadbeef};
<br>    uint8_t* byte = (uint8_t*)&data[0];
<br>    uint8_t value = byte[1];
<br>#else
<br>#error "Define VARIANT={1,2,3,4}"
<br>#endif
<br>    return value;
<br>}<br>
<br></span></div><div><font face="arial, helvetica, sans-serif">Now, when I throw Clang's static analysis at it with VARIANT 1,2, or 3 it says everything's a-OK.  But with VARIANT=4 it complains:</font></div><div><span style="font-family:monospace"><br></span></div><div><font face="monospace, monospace">$</font><span style="font-family:monospace"><span style="font-weight:bold;color:rgb(84,255,84)"> </span><span style="color:rgb(0,0,0)">scan-build-3.8 --use-cc=clang-3.8 /usr/share/clang/scan-build-3.8/libexec/ccc-analyzer -D VARIANT=4 problem.c</span><br></span></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">scan-build: Using '/usr/lib/llvm-3.8/bin/clang' for static analysis
</span><br>problem.c:19:5: warning: Assigned value is garbage or undefined
<br>    uint8_t value = byte[1];
<br>    ^~~~~~~~~~~~~   ~~~~~~~
<br>1 warning generated.
<br>scan-build: 1 bug found.
<br>scan-build: Run 'scan-view /tmp/scan-build-2016-06-25-104600-17811-1' to examine bug reports.<br></span></div><div><span style="font-family:monospace"><br></span></div><div><font face="arial, helvetica, sans-serif">My question is why is byte[1] undefined in VARIANT 4 but not anywhere else?  I would think if it's complaining that the value is dependent on endianness, then they should all be reported.  Is there some detail of the C spec that I'm missing, or have I stumbled on a false positive (would be a first for me -- every other issue reported has been legit thus far).</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">Thanks,</font></div><div><font face="arial, helvetica, sans-serif">-Andrew</font></div></div>