Re: [hackers] [sbase] Do not rely on `s' being incremented before taking the address of it || sin

From: Rob <robpilling_AT_gmail.com>
Date: Wed, 9 Oct 2013 20:22:26 +0100

On Wed, Oct 09, 2013 at 05:44:08PM +0200, git_AT_suckless.org wrote:
> commit 484d5b434057e82275371f7fe64ef5ae6dfcb82c
> Author: sin <sin_AT_2f30.org>
> Date: Wed Oct 9 16:28:04 2013 +0100
>
> Do not rely on `s' being incremented before taking the address of it
>
> The order of evaluation for the arguments of a function is not
> defined by the standard.

> strtoul(++s, &s, 10)

Funnily enough I was doing something similar recently and wondered about
that - pretty sure this isn't undefined behaviour, ++s doesn't affect
the address of s.

There is only one read and write to `s' going on here, and all the
standard says on this is that the write must be performed before the
call-sequence-point.

Relevant bit of standardese:

"An expression is a sequence of operators and operands that specifies
computation of a value, or that designates an object or a function, or
that generates side effects, or that performs a combination thereof. The
value computations of the operands of an operator are sequenced before
the value computation of the result of the operator.

If a side effect on a scalar object is unsequenced relative to either a
different side effect on the same scalar object or a value computation
using the value of the same scalar object, the behavior is undefined. If
there are multiple allowable orderings of the subexpressions of an
expression, the behavior is undefined if such an unsequenced side effect
occurs in any of the orderings"

[C11 6.5.1-2]

There's only one side effect/value computation on `s', so that's those
paragraphs out of the way, so we can cut the 10 lines back down to 2.

Cheers,
Rob
Received on Wed Oct 09 2013 - 21:22:26 CEST

This archive was generated by hypermail 2.3.0 : Wed Oct 09 2013 - 21:24:17 CEST