I was running some mutation tests on this C-testsuite. One of my mutations caused the following test 00143 to either seg fault, or to run to completion with a stack smashing detected error. My mutation changed n = (count+7) / 8;
to n = (count+7) * 8;
I have posted the mutated code below:
#include <stdio.h>
int main()
{
int count, n;
short *from, *to;
short a[39], b[39];
for(n = 0; n < 39; n++) {
a[n] = n;
b[n] = 0;
}
from = a;
to = b;
count = 39;
n = (count + 7) * 8;
switch (count % 8) {
case 0: do { *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while (--n > 0);
}
for(n = 0; n < 39; n++)
if(a[n] != b[n])
return 1;
return 0;
}
You can see that n
should not go out of bounds as the last for loop is between 0 <= n < 39
.
My question is why does a segmentation fault, or stack smashing occur if I am not accessing out of bound arrays? Additionally why do I get flaky behavior?