ソースを参照

FFI: Perform stricter checks in ffi.cast(). Record ffi.cast().

Mike Pall 14 年 前
コミット
ceece4d13e
1 ファイル変更6 行追加7 行削除
  1. 6 7
      src/lib_ffi.c

+ 6 - 7
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);
   }