[SystemSafety] A small taste of what we're up against

Olwen Morgan olwen at phaedsys.com
Wed Oct 24 17:28:10 CEST 2018


And now, for one last time, because the hypomania is at last subsiding:

#include <stdio.h>

int main (void)
{
     int a[2] = {1, 2};

     const int i1 = ~(1^1);
     const int i2 = ((1^1)-1);

     int       e0 = a[~(1^1)];
     int       e1 = a[((1^1)-1)];

     int       f0 = a[i1];
     int       f1 = a[i2];

     printf("\ni1 = %i, i2 = %i\n", i1, i2);
     printf("e0 = %i, e1 = %i\n", e0, e1);
     printf("f0 = %i, f1 = %i\n", f0, f1);

     return 0;
}

gcc and tcc give no diagnostics,

cppcheck gives:

... test-cppcheck-1.c:11: error: Array index -1 is out of bounds.

... test-cppcheck-1.c:14: error: Array index -1 is out of bounds.

clang gives:

test-cppcheck-1.c:10:17: warning: array index -1 is before the beginning 
of the array [-Warray-bounds]
         int       e0 = a[~(1^1)];
                        ^ ~~~~~~
test-cppcheck-1.c:5:2: note: array 'a' declared here
         int a[2] = {1, 2};
         ^
test-cppcheck-1.c:11:17: warning: array index -1 is before the beginning 
of the array [-Warray-bounds]
         int       e1 = a[((1^1)-1)];
                        ^  ~~~~~~~
test-cppcheck-1.c:5:2: note: array 'a' declared here
         int a[2] = {1, 2};
         ^
test-cppcheck-1.c:13:17: warning: array index -1 is before the beginning 
of the array [-Warray-bounds]
         int       f0 = a[i1];
                        ^ ~~
test-cppcheck-1.c:5:2: note: array 'a' declared here
         int a[2] = {1, 2};
         ^
test-cppcheck-1.c:14:17: warning: array index -1 is before the beginning 
of the array [-Warray-bounds]
         int       f1 = a[i2];
                        ^ ~~
test-cppcheck-1.c:5:2: note: array 'a' declared here
         int a[2] = {1, 2};
         ^

running compiled code from each of tcc and clang gives:

i1 = -1, i2 = -1
e0 = -1, e1 = -1
f0 = -1, f1 = -1

running code from gcc gives:

i1 = -1, i2 = -1
e0 = 0, e1 = 0
f0 = 0, f1 = 0

By the common law right of acronymia vulgaria, I pronounce that cppcheck 
stands for Can't Perform Proper CHECKing.

Tin hats on, folks.

Olwen










More information about the systemsafety mailing list