lua-users home
lua-l archive

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


> The storage alignment of strings and userdata are both enforced
> through a union with the `L_Umaxalign` structure.  For luaL_Buffer, an
> allocated buffer must be aligned by the `allocf` (as are strings and
> userdata), but there is the case where luaL_Buffer is a static
> variable or on the stack, and in this case there is no enforcement.
> With the current definition it must be at least 4 byte aligned on 32
> bit systems (8 byte on 64 bit) because it follows a pointer.
> 
> Given that we can store binary data in strings in Lua, and this can
> safely be extracted from a string due to its alignment requirement,
> can we please have the same alignment guarantee on the luaL_Buffer
> `initb` field so that we can build binary structures for storage in
> strings through this API.
> 
> For my use I just converted the `char initb[LUAL_BUFFERSIZE];` field
> to `union { L_Umaxalign dummy; char b[LUAL_BUFFERSIZE]; } init;` but I
> had to shuffle some header file definitions to make everything visible
> as required.  I then just changed any occurrence of `initb` with
> `init.b`.  Not sure if this is the best solution.

This sounds a reasonable fix. (What was the "shuffle" you did?
A natural place for L_Umaxalign would be luaconf.h, but luaconf has
no language definitions at all, only macros.)

Just for curiosity, did you have a real problem with this alignment?

-- Roberto