diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2023-01-21 07:51:14 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-21 21:21:14 +0530 |
commit | 59289255411b902588619fd7d2f6e3e48af11d82 (patch) | |
tree | a328e565abb1695e57c99538d6482f62e5008cad /ext/ffi/callback.rs | |
parent | 638b6ef554676422c43cc5c0ae2285ba369740bf (diff) |
fix(ext/ffi): disallow empty ffi structs (#17487)
This patch makes `NativeType` to `libffi::middle::Type` conversion
failliable and w.t disallows struct with empty fields. libffi does not
handle "empty" struct because they don't exist in C (or Rust).
Fixes #17481
Diffstat (limited to 'ext/ffi/callback.rs')
-rw-r--r-- | ext/ffi/callback.rs | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/ext/ffi/callback.rs b/ext/ffi/callback.rs index d6ef51823..1558d950e 100644 --- a/ext/ffi/callback.rs +++ b/ext/ffi/callback.rs @@ -40,18 +40,19 @@ pub struct PtrSymbol { } impl PtrSymbol { - pub fn new(fn_ptr: usize, def: &ForeignFunction) -> Self { + pub fn new(fn_ptr: usize, def: &ForeignFunction) -> Result<Self, AnyError> { let ptr = libffi::middle::CodePtr::from_ptr(fn_ptr as _); let cif = libffi::middle::Cif::new( def .parameters .clone() .into_iter() - .map(libffi::middle::Type::from), - def.result.clone().into(), + .map(libffi::middle::Type::try_from) + .collect::<Result<Vec<_>, _>>()?, + def.result.clone().try_into()?, ); - Self { cif, ptr } + Ok(Self { cif, ptr }) } } @@ -578,8 +579,12 @@ where waker: None, })); let cif = Cif::new( - args.parameters.into_iter().map(libffi::middle::Type::from), - libffi::middle::Type::from(args.result), + args + .parameters + .into_iter() + .map(libffi::middle::Type::try_from) + .collect::<Result<Vec<_>, _>>()?, + libffi::middle::Type::try_from(args.result)?, ); // SAFETY: CallbackInfo is leaked, is not null and stays valid as long as the callback exists. |