1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-07 06:46:59 -05:00

fix(ext/ffi): Callbacks panic on returning isize (#19022)

This commit is contained in:
Aapo Alasuutari 2023-05-07 17:27:16 +03:00 committed by David Sherret
parent 1b610bfdad
commit 6e72ae7a09

View file

@ -320,17 +320,6 @@ unsafe fn do_ffi_callback(
}; };
*(result as *mut bool) = value; *(result as *mut bool) = value;
} }
NativeType::I32 => {
let value = if let Ok(value) = v8::Local::<v8::Integer>::try_from(value) {
value.value() as i32
} else {
// Fallthrough, probably UB.
value
.int32_value(scope)
.expect("Unable to deserialize result parameter.")
};
*(result as *mut i32) = value;
}
NativeType::F32 => { NativeType::F32 => {
let value = if let Ok(value) = v8::Local::<v8::Number>::try_from(value) { let value = if let Ok(value) = v8::Local::<v8::Number>::try_from(value) {
value.value() as f32 value.value() as f32
@ -392,7 +381,7 @@ unsafe fn do_ffi_callback(
*(result as *mut *mut c_void) = pointer; *(result as *mut *mut c_void) = pointer;
} }
NativeType::I8 => { NativeType::I8 => {
let value = if let Ok(value) = v8::Local::<v8::Integer>::try_from(value) { let value = if let Ok(value) = v8::Local::<v8::Int32>::try_from(value) {
value.value() as i8 value.value() as i8
} else { } else {
// Fallthrough, essentially UB. // Fallthrough, essentially UB.
@ -403,7 +392,7 @@ unsafe fn do_ffi_callback(
*(result as *mut i8) = value; *(result as *mut i8) = value;
} }
NativeType::U8 => { NativeType::U8 => {
let value = if let Ok(value) = v8::Local::<v8::Integer>::try_from(value) { let value = if let Ok(value) = v8::Local::<v8::Uint32>::try_from(value) {
value.value() as u8 value.value() as u8
} else { } else {
// Fallthrough, essentially UB. // Fallthrough, essentially UB.
@ -414,7 +403,7 @@ unsafe fn do_ffi_callback(
*(result as *mut u8) = value; *(result as *mut u8) = value;
} }
NativeType::I16 => { NativeType::I16 => {
let value = if let Ok(value) = v8::Local::<v8::Integer>::try_from(value) { let value = if let Ok(value) = v8::Local::<v8::Int32>::try_from(value) {
value.value() as i16 value.value() as i16
} else { } else {
// Fallthrough, essentially UB. // Fallthrough, essentially UB.
@ -425,7 +414,7 @@ unsafe fn do_ffi_callback(
*(result as *mut i16) = value; *(result as *mut i16) = value;
} }
NativeType::U16 => { NativeType::U16 => {
let value = if let Ok(value) = v8::Local::<v8::Integer>::try_from(value) { let value = if let Ok(value) = v8::Local::<v8::Uint32>::try_from(value) {
value.value() as u16 value.value() as u16
} else { } else {
// Fallthrough, essentially UB. // Fallthrough, essentially UB.
@ -435,9 +424,20 @@ unsafe fn do_ffi_callback(
}; };
*(result as *mut u16) = value; *(result as *mut u16) = value;
} }
NativeType::I32 => {
let value = if let Ok(value) = v8::Local::<v8::Int32>::try_from(value) {
value.value()
} else {
// Fallthrough, essentially UB.
value
.int32_value(scope)
.expect("Unable to deserialize result parameter.")
};
*(result as *mut i32) = value;
}
NativeType::U32 => { NativeType::U32 => {
let value = if let Ok(value) = v8::Local::<v8::Integer>::try_from(value) { let value = if let Ok(value) = v8::Local::<v8::Uint32>::try_from(value) {
value.value() as u32 value.value()
} else { } else {
// Fallthrough, essentially UB. // Fallthrough, essentially UB.
value value
@ -446,21 +446,25 @@ unsafe fn do_ffi_callback(
}; };
*(result as *mut u32) = value; *(result as *mut u32) = value;
} }
NativeType::I64 => { NativeType::I64 | NativeType::ISize => {
if let Ok(value) = v8::Local::<v8::BigInt>::try_from(value) { if let Ok(value) = v8::Local::<v8::BigInt>::try_from(value) {
*(result as *mut i64) = value.i64_value().0; *(result as *mut i64) = value.i64_value().0;
} else if let Ok(value) = v8::Local::<v8::Integer>::try_from(value) { } else if let Ok(value) = v8::Local::<v8::Int32>::try_from(value) {
*(result as *mut i64) = value.value(); *(result as *mut i64) = value.value() as i64;
} else if let Ok(value) = v8::Local::<v8::Number>::try_from(value) {
*(result as *mut i64) = value.value() as i64;
} else { } else {
*(result as *mut i64) = value *(result as *mut i64) = value
.integer_value(scope) .integer_value(scope)
.expect("Unable to deserialize result parameter."); .expect("Unable to deserialize result parameter.");
} }
} }
NativeType::U64 => { NativeType::U64 | NativeType::USize => {
if let Ok(value) = v8::Local::<v8::BigInt>::try_from(value) { if let Ok(value) = v8::Local::<v8::BigInt>::try_from(value) {
*(result as *mut u64) = value.u64_value().0; *(result as *mut u64) = value.u64_value().0;
} else if let Ok(value) = v8::Local::<v8::Integer>::try_from(value) { } else if let Ok(value) = v8::Local::<v8::Uint32>::try_from(value) {
*(result as *mut u64) = value.value() as u64;
} else if let Ok(value) = v8::Local::<v8::Number>::try_from(value) {
*(result as *mut u64) = value.value() as u64; *(result as *mut u64) = value.value() as u64;
} else { } else {
*(result as *mut u64) = value *(result as *mut u64) = value
@ -501,9 +505,6 @@ unsafe fn do_ffi_callback(
NativeType::Void => { NativeType::Void => {
// nop // nop
} }
_ => {
unreachable!();
}
}; };
} }