Avoid casts to 'union GCUnion*'

The union may have alignment requirements stricter than some of its
members. Some checking tools (e.g., gcc with options -fsanitize) can
then complain that the result of a cast from pointer to member to
pointer to the union is misaligned.
This commit is contained in:
Roberto I 2026-07-21 16:51:04 -03:00
parent 84938a7d2b
commit 9130ceb19d

View file

@ -383,7 +383,7 @@ typedef struct global_State {
/* /*
** Union of all collectable objects (only for conversions) ** Union of all collectable objects
** ISO C99, 6.5.2.3 p.5: ** ISO C99, 6.5.2.3 p.5:
** "if a union contains several structures that share a common initial ** "if a union contains several structures that share a common initial
** sequence [...], and if the union object currently contains one ** sequence [...], and if the union object currently contains one
@ -403,32 +403,32 @@ union GCUnion {
}; };
/* /* macros to convert a GCObject into a specific value
** ISO C99, 6.7.2.1 p.14: ** ISO C99, 6.3.2.2 p.7:
** "A pointer to a union object, suitably converted, points to each of ** "A pointer to an object or incomplete type may be converted to a
** its members [...], and vice versa." ** pointer to a different object or incomplete type. If the resulting
** pointer is not correctly aligned for the pointed-to type, the
** behavior is undefined. Otherwise, when converted back again, the
** result shall compare equal to the original pointer."
*/ */
#define cast_u(o) cast(union GCUnion *, (o)) #define gco2(v,T,o) check_exp((o)->tt == v, cast(T*, o))
#define gco2nv(t,T,o) check_exp(novariant((o)->tt) == t, cast(T*, o))
/* macros to convert a GCObject into a specific value */ #define gco2ts(o) gco2nv(LUA_TSTRING, TString, o)
#define gco2ts(o) \ #define gco2u(o) gco2(LUA_VUSERDATA, Udata, o)
check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts)) #define gco2lcl(o) (&gco2(LUA_VLCL, Closure, o)->l)
#define gco2u(o) check_exp((o)->tt == LUA_VUSERDATA, &((cast_u(o))->u)) #define gco2ccl(o) (&gco2(LUA_VCCL, Closure, o)->c)
#define gco2lcl(o) check_exp((o)->tt == LUA_VLCL, &((cast_u(o))->cl.l)) #define gco2cl(o) gco2nv(LUA_TFUNCTION, Closure, o)
#define gco2ccl(o) check_exp((o)->tt == LUA_VCCL, &((cast_u(o))->cl.c)) #define gco2t(o) gco2(LUA_VTABLE, Table, o)
#define gco2cl(o) \ #define gco2p(o) gco2(LUA_VPROTO, Proto, o)
check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl)) #define gco2th(o) gco2(LUA_VTHREAD, lua_State, o)
#define gco2t(o) check_exp((o)->tt == LUA_VTABLE, &((cast_u(o))->h)) #define gco2upv(o) gco2(LUA_VUPVAL, UpVal, o)
#define gco2p(o) check_exp((o)->tt == LUA_VPROTO, &((cast_u(o))->p))
#define gco2th(o) check_exp((o)->tt == LUA_VTHREAD, &((cast_u(o))->th))
#define gco2upv(o) check_exp((o)->tt == LUA_VUPVAL, &((cast_u(o))->upv))
/* /*
** macro to convert a Lua object into a GCObject ** macro to convert a Lua object into a GCObject
*/ */
#define obj2gco(v) \ #define obj2gco(v) \
check_exp(novariant((v)->tt) >= LUA_TSTRING, &(cast_u(v)->gc)) check_exp(novariant((v)->tt) >= LUA_TSTRING, cast(GCObject*, v))
/* actual number of total memory allocated */ /* actual number of total memory allocated */