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

Olwen Morgan olwen at phaedsys.com
Wed Oct 24 09:13:02 CEST 2018


Just a quickie:

if, in the code below, you replace:


     PrintEvalOrder((a[0]=++i), (a[1]=++i), (a[2]=++i));

with:


     PrintEvalOrder((++i), (++i), (++i));

both clang and tcc tell you the order of evaluation is p1, p2, p3 
whereas gcc says it's p3, p3, p3. ... WTF?

Presumably, this is due to over-zealous optimisation,


Support destructive testing of software !

Olwen


On 23/10/2018 18:44, Olwen Morgan wrote:
>
>
> 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
>
>
> _______________________________________________
> The System Safety Mailing List
> systemsafety at TechFak.Uni-Bielefeld.DE
>


More information about the systemsafety mailing list