From ceece4d13e6da6b96060d7050122f7ed40d75cc9 Mon Sep 17 00:00:00 2001 From: Mike Pall Date: Sun, 6 Feb 2011 12:46:18 +0100 Subject: [PATCH] FFI: Perform stricter checks in ffi.cast(). Record ffi.cast(). --- src/lib_ffi.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/lib_ffi.c b/src/lib_ffi.c index 54d49f87..26e18183 100644 --- a/src/lib_ffi.c +++ b/src/lib_ffi.c @@ -406,19 +406,18 @@ LJLIB_CF(ffi_offsetof) return 0; } -LJLIB_CF(ffi_cast) +LJLIB_CF(ffi_cast) LJLIB_REC(ffi_new) { CTState *cts = ctype_cts(L); CTypeID id = ffi_checkctype(L, cts); + CType *d = ctype_raw(cts, id); TValue *o = lj_lib_checkany(L, 2); L->top = o+1; /* Make sure this is the last item on the stack. */ + if (!(ctype_isnum(d->info) || ctype_isptr(d->info) || ctype_isenum(d->info))) + lj_err_arg(L, 1, LJ_ERR_FFI_INVTYPE); if (!(tviscdata(o) && cdataV(o)->typeid == id)) { - CTSize sz = lj_ctype_size(cts, id); - GCcdata *cd; - if (sz == CTSIZE_INVALID) - lj_err_caller(L, LJ_ERR_FFI_INVSIZE); - cd = lj_cdata_new(cts, id, sz); /* Create destination cdata. */ - lj_cconv_ct_tv(cts, ctype_raw(cts, id), cdataptr(cd), o, CCF_CAST); + GCcdata *cd = lj_cdata_new(cts, id, d->size); + lj_cconv_ct_tv(cts, d, cdataptr(cd), o, CCF_CAST); setcdataV(L, o, cd); lj_gc_check(L); }