[SystemSafety] A small taste of what we're up against
Olwen Morgan
olwen at phaedsys.com
Tue Oct 23 19:44:03 CEST 2018
Another WTF C program:
#include <stdio.h>
void PrintEvalOrder(int p1, int p2, int p3)
{
printf("\n p1, p2, p3 evaluated in order: p%i, p%i, p%i\n", p1, p2, p3);
return;
}
int main(void)
{
int i = 0;
int a[3] = {0, 0, 0};
/* next line tests order of eval. of args to a function call */
PrintEvalOrder((a[0]=++i), (a[1]=++i), (a[2]=++i));
return 60||0x2C; /* to be read as text :-0 */
}
Code compiled with gcc-5.4.0 outputs: "p1, p2, p3 evaluated in order:
p3, p2, p1"
Code compiled with clang-4.0 outputs: "p1, p2, p3 evaluated in order:
p1, p2, p3"
Code compiled with tcc v 0.9.26 outputs: "p1, p2, p3 evaluated in order:
p1, p2, p3"
gcc and clang give diagnostics for the multiple side effects but tcc
gives none.
cppcheck gives the diagnostics:
... test-ccpcheck-5.c:12: error: Expression 'a[0]=++i,a[1]=++i' depends
on order of evaluation of side effects
... test-ccpcheck-5.c:12: error: Expression 'a[0]=++i,a[1]=++i,a[2]=++i'
depends on order of evaluation of side effects
... test-ccpcheck-5.c:12: style: Variable 'a' is assigned a value that
is never used.
As regards cppcheck, I'm not sure whether I'm thinking WTF? or
ROTFLMAO!(probably because I've been spoiled using QAC). Where did
cppcheck get the idea that values in 'a' aren't used?
@ Derek J: I'd never considered testing with JIT compilers because they
aren't much used in the small-end embedded world. It's always an
eye-opener when you see new compiler imbecilities.
Olwen
More information about the systemsafety
mailing list