In the Solidity documentation is stated that:
The evaluation order of expressions is not specified (more formally, the order in which the children of one node in the expression tree are evaluated is not specified, but they are of course evaluated before the node itself). It is only guaranteed that statements are executed in order and short-circuiting for boolean expressions is done. See Order of Precedence of Operators for more information.
I want to better understand this point with the aid of an example.
Let's say I have a smart contract with a function g
that computes the sum of two inputs.
g(uint a, uint b) {
return a + b;
}
...
uint res = g(x + 2, (x++) + 3);
Let us further suppose that x
is equals to 5.
So, since the evaluation order is not guaranteed it is possible that the compiler will compile this instruction into two different piece of code:
either the actual parameter for a
is evaluated before the one for b
or the actual parameter for b
before the one of a
.
This will give raise to two different calls with different result:
g(7, 8)
g(8, 8)
Is this example correct? Why is this unsafe behavior kept in a language that is supposed to enable to write safe smart contracts?
(x++) + 3
, which is not something that anyone is ever likely to use (other than giving it as an example of how not to write code).