Re: Infinite loop in dcraw with current GCC versions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 2015-02-27 at 14:02 -0500, dcoffin@xxxxxxxxxxxxxxxxxx wrote:
> There is no "undefined behavior" here.  K&R defined it
> very clearly:  Arrays are pointers, and square brackets are
> syntactic sugar for pointer arithmetic and dereferencing:
> 
>         cam_xyz[i][j]  vs.  *(cam_xyz + (i)*3 + (j))

The ISO C standard Section 6.5.2.1 Array Subscripting appears to agree
with GCC, that this is undefined behavior.  This is from the online copy
of n1570 because my copy of the original ANSI standard is at work:

> Successive subscript operators designate an element of a
> multidimensional array object. If E is an n-dimensional array (n ≥ 2)
> with dimensions i × j × ... × k, then E (used as other than an lvalue)
> is converted to a pointer to an (n − 1)-dimensional array with
> dimensions j × ... × k. If the unary * operator is applied to this
> pointer explicitly, or implicitly as a result of subscripting, the
> result is the referenced (n − 1)-dimensional array, which itself is
> converted into a pointer if used as other than an lvalue.

> EXAMPLE  Consider the array object defined by the declaration
>    int x[3][5];
> Here x is a 3 × 5 array of ints; more precisely, x is an array of
> three element objects, each of which is an array of five ints. In the
> expression x[i], which is equivalent to (*((x)+(i))), x is first
> converted to a pointer to the initial array of five ints. Then i is
> adjusted according to the type of x, which conceptually entails
> multiplying i by the size of the object to which the pointer points,
> namely an array of five int objects. The results are added and
> indirection is applied to yield an array of five ints

The standard expands the object one dimension at a time and the result
at each step must be defined: "If E is an n-dimensional array (n ≥ 2)
with dimensions i × j × ... × k, then E (used as other than an lvalue)
is converted to a pointer to an (n − 1)-dimensional array with
dimensions j × ... × k" and "x is an array of three element objects,
each of which is an array of five ints".

So attempting to access beyond the end of any individual dimension of a
multi-dimensional array is undefined behavior.





[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux