]> git.ipfire.org Git - thirdparty/LuaJIT.git/commitdiff
FFI: Perform stricter checks in ffi.cast(). Record ffi.cast().
authorMike Pall <mike>
Sun, 6 Feb 2011 11:46:18 +0000 (12:46 +0100)
committerMike Pall <mike>
Sun, 6 Feb 2011 11:46:18 +0000 (12:46 +0100)
src/lib_ffi.c

index 54d49f87d0e77ba9d65127f336d27a8900ae90db..26e18183038e905c58aff215c7f8d5445c0a3273 100644 (file)
@@ -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);
   }