Skip to content

Commit 11ebe52

Browse files
committed
bridge: When converting panics to errors, check for String too
See rust-lang/rfcs#1389.
1 parent 58b5256 commit 11ebe52

File tree

5 files changed

+23
-22
lines changed

5 files changed

+23
-22
lines changed

rust/bridge/jni/src/logging.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
use jni::objects::{GlobalRef, JClass, JObject, JValue};
77
use jni::sys::jint;
88
use jni::{JNIEnv, JavaVM};
9-
use libsignal_bridge::jni_signature;
10-
use std::any::Any;
9+
use libsignal_bridge::{describe_panic, jni_signature};
1110
use std::panic::{catch_unwind, AssertUnwindSafe};
1211
use std::process::abort;
1312

@@ -118,17 +117,6 @@ impl log::Log for JniLogger {
118117
fn flush(&self) {}
119118
}
120119

121-
// See https://github.com/rust-lang/rfcs/issues/1389
122-
fn describe_panic(any: &Box<dyn Any + Send>) -> String {
123-
if let Some(msg) = any.downcast_ref::<&str>() {
124-
msg.to_string()
125-
} else if let Some(msg) = any.downcast_ref::<String>() {
126-
msg.to_string()
127-
} else {
128-
"(break on rust_panic to debug)".to_string()
129-
}
130-
}
131-
132120
/// A low-level version of `run_ffi_safe` that just aborts on errors.
133121
///
134122
/// This is important for logging failures because we might want to log during the normal

rust/bridge/shared/src/ffi/error.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use device_transfer::Error as DeviceTransferError;
1010
use libsignal_protocol::*;
1111
use signal_crypto::Error as SignalCryptoError;
1212

13+
use crate::support::describe_panic;
14+
1315
/// The top-level error type (opaquely) returned to C clients when something goes wrong.
1416
#[derive(Debug)]
1517
pub enum SignalFfiError {
@@ -39,11 +41,9 @@ impl fmt::Display for SignalFfiError {
3941
SignalFfiError::InsufficientOutputSize(n, h) => {
4042
write!(f, "needed {} elements only {} provided", n, h)
4143
}
42-
43-
SignalFfiError::UnexpectedPanic(e) => match e.downcast_ref::<&'static str>() {
44-
Some(s) => write!(f, "unexpected panic: {}", s),
45-
None => write!(f, "unknown unexpected panic"),
46-
},
44+
SignalFfiError::UnexpectedPanic(e) => {
45+
write!(f, "unexpected panic: {}", describe_panic(e))
46+
}
4747
}
4848
}
4949
}

rust/bridge/shared/src/jni/error.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use hsm_enclave::Error as HsmEnclaveError;
1212
use libsignal_protocol::*;
1313
use signal_crypto::Error as SignalCryptoError;
1414

15+
use crate::support::describe_panic;
16+
1517
use super::*;
1618

1719
/// The top-level error type for when something goes wrong.
@@ -47,10 +49,9 @@ impl fmt::Display for SignalJniError {
4749
SignalJniError::HsmEnclave(e) => {
4850
write!(f, "{}", e)
4951
}
50-
SignalJniError::UnexpectedPanic(e) => match e.downcast_ref::<&'static str>() {
51-
Some(s) => write!(f, "unexpected panic: {}", s),
52-
None => write!(f, "unknown unexpected panic"),
53-
},
52+
SignalJniError::UnexpectedPanic(e) => {
53+
write!(f, "unexpected panic: {}", describe_panic(e))
54+
}
5455
}
5556
}
5657
}

rust/bridge/shared/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub mod node;
2323

2424
#[macro_use]
2525
mod support;
26+
pub use support::describe_panic;
2627

2728
pub mod crypto;
2829
pub mod protocol;

rust/bridge/shared/src/support/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ pub(crate) use paste::paste;
88
mod transform_helper;
99
pub(crate) use transform_helper::*;
1010

11+
// See https://github.com/rust-lang/rfcs/issues/1389
12+
pub fn describe_panic(any: &Box<dyn std::any::Any + Send>) -> String {
13+
if let Some(msg) = any.downcast_ref::<&str>() {
14+
msg.to_string()
15+
} else if let Some(msg) = any.downcast_ref::<String>() {
16+
msg.to_string()
17+
} else {
18+
"(break on rust_panic to debug)".to_string()
19+
}
20+
}
21+
1122
/// Exposes a Rust type to each of the bridges as a boxed value.
1223
///
1324
/// Full form:

0 commit comments

Comments
 (0)