[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua define ARRAY_FLEXIBLE_MEMBER
- From: Steffen Nurpmeso <steffen@...>
- Date: Tue, 15 Nov 2022 17:44:47 +0100
Roberto Ierusalimschy wrote in
<20221115143157.GA17473@arraial.inf.puc-rio.br>:
|> Hm, i hate reading ISO standard so the exact wording i do not
|> know, but the above makes me think you do enter undefined area by
|> storing more in .data than is allowed (likely depending on
|> C compiler flags and optimization too, i have not looked what Lua
|> uses).
|
|There is nothing undefined here; it is all valid ISO C. The code
|does not store anything *in* 'data'. When we write cl->data[i], that
|means *(cl->data + i). It only gets the address and type of 'cl->data',
|the rest is pointer arithmetic. As long as the result of the addition
|is aligned, has the correct type, and points inside the same object,
|everything is well defined.
Ah sigh, do not make me weird; now i had to look. Well yes i also
use that idiom, but then without any array field in the struct as
such (ie alloc as much as needed, place the struct at the bottom
and then ptr=&type[1] to point to thereafter).
Ok as you write it there above (grep in Lua 5.4.4 did not reveal
anything .. i was not following this thread .. by the way, i am
still impressed and have lua-svg around for looking! Quite a bit
of C++ in there) i was happy for search engines.
https://wiki.sei.cmu.edu/confluence/display/c/ARR30-C.+Do+not+form+or+use+out-of-bounds+pointers+or+array+subscripts
Thinking about it however, and given that i now recall the word
"offsetof", i am pretty sure you are about to do
((char*)ptr + offsetof(array-field) - sizeof(array-field))[index].
Sorry for the noise.
--steffen
|
|Der Kragenbaer, The moon bear,
|der holt sich munter he cheerfully and one by one
|einen nach dem anderen runter wa.ks himself off
|(By Robert Gernhardt)