Skip to content

Commit 9130ceb

Browse files
committed
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.
1 parent 84938a7 commit 9130ceb

1 file changed

Lines changed: 20 additions & 20 deletions

File tree

lstate.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ typedef struct global_State {
383383

384384

385385
/*
386-
** Union of all collectable objects (only for conversions)
386+
** Union of all collectable objects
387387
** ISO C99, 6.5.2.3 p.5:
388388
** "if a union contains several structures that share a common initial
389389
** sequence [...], and if the union object currently contains one
@@ -403,32 +403,32 @@ union GCUnion {
403403
};
404404

405405

406-
/*
407-
** ISO C99, 6.7.2.1 p.14:
408-
** "A pointer to a union object, suitably converted, points to each of
409-
** its members [...], and vice versa."
406+
/* macros to convert a GCObject into a specific value
407+
** ISO C99, 6.3.2.2 p.7:
408+
** "A pointer to an object or incomplete type may be converted to a
409+
** pointer to a different object or incomplete type. If the resulting
410+
** pointer is not correctly aligned for the pointed-to type, the
411+
** behavior is undefined. Otherwise, when converted back again, the
412+
** result shall compare equal to the original pointer."
410413
*/
411-
#define cast_u(o) cast(union GCUnion *, (o))
412-
413-
/* macros to convert a GCObject into a specific value */
414-
#define gco2ts(o) \
415-
check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
416-
#define gco2u(o) check_exp((o)->tt == LUA_VUSERDATA, &((cast_u(o))->u))
417-
#define gco2lcl(o) check_exp((o)->tt == LUA_VLCL, &((cast_u(o))->cl.l))
418-
#define gco2ccl(o) check_exp((o)->tt == LUA_VCCL, &((cast_u(o))->cl.c))
419-
#define gco2cl(o) \
420-
check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
421-
#define gco2t(o) check_exp((o)->tt == LUA_VTABLE, &((cast_u(o))->h))
422-
#define gco2p(o) check_exp((o)->tt == LUA_VPROTO, &((cast_u(o))->p))
423-
#define gco2th(o) check_exp((o)->tt == LUA_VTHREAD, &((cast_u(o))->th))
424-
#define gco2upv(o) check_exp((o)->tt == LUA_VUPVAL, &((cast_u(o))->upv))
414+
#define gco2(v,T,o) check_exp((o)->tt == v, cast(T*, o))
415+
#define gco2nv(t,T,o) check_exp(novariant((o)->tt) == t, cast(T*, o))
416+
#define gco2ts(o) gco2nv(LUA_TSTRING, TString, o)
417+
#define gco2u(o) gco2(LUA_VUSERDATA, Udata, o)
418+
#define gco2lcl(o) (&gco2(LUA_VLCL, Closure, o)->l)
419+
#define gco2ccl(o) (&gco2(LUA_VCCL, Closure, o)->c)
420+
#define gco2cl(o) gco2nv(LUA_TFUNCTION, Closure, o)
421+
#define gco2t(o) gco2(LUA_VTABLE, Table, o)
422+
#define gco2p(o) gco2(LUA_VPROTO, Proto, o)
423+
#define gco2th(o) gco2(LUA_VTHREAD, lua_State, o)
424+
#define gco2upv(o) gco2(LUA_VUPVAL, UpVal, o)
425425

426426

427427
/*
428428
** macro to convert a Lua object into a GCObject
429429
*/
430430
#define obj2gco(v) \
431-
check_exp(novariant((v)->tt) >= LUA_TSTRING, &(cast_u(v)->gc))
431+
check_exp(novariant((v)->tt) >= LUA_TSTRING, cast(GCObject*, v))
432432

433433

434434
/* actual number of total memory allocated */

0 commit comments

Comments
 (0)