Skip to content

Commit

Permalink
Merge pull request #204 from expressvpn/CVPN-1637-remove-unreachable
Browse files Browse the repository at this point in the history
Reduce use of `unreachable!(...)`
  • Loading branch information
xv-ian-c authored Dec 5, 2024
2 parents bccc8d8 + d94d063 commit ed93b38
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion wolfssl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn enable_debugging(on: bool) {
wolfssl_sys::wolfCrypt_ErrorCodes_NOT_COMPILED_IN => {
panic!("Inconsistent build, debug not enabled in wolfssl_sys")
}
e => unreachable!("{e:?}"),
e => unreachable!("wolfSSL_Debugging_ON: {e:?}"),
}
} else {
// SAFETY: [`wolfSSL_Debugging_OFF`][0] ([also][1]) has no safety concerns as per documentation
Expand Down
58 changes: 34 additions & 24 deletions wolfssl/src/ssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
match unsafe { wolfssl_sys::wolfSSL_is_init_finished(self.ssl.as_ptr()) } {
0 => false,
1 => true,
e => unreachable!("{e:?}"),
e => unreachable!("wolfSSL_is_init_finished: {e:?}"),
}
}

Expand Down Expand Up @@ -403,7 +403,7 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
}
e => Err(Error::fatal(e)),
},
e => unreachable!("{e:?}"),
e => Err(Error::fatal(e)),
}
}

Expand Down Expand Up @@ -444,7 +444,7 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
}
x => Err(Error::fatal(x)),
},
e => unreachable!("{e:?}"),
e => Err(Error::fatal(e)),
}
}

Expand Down Expand Up @@ -566,7 +566,7 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
} {
0 => false,
1 => true,
e => unreachable!("{e:?}"),
e => unreachable!("wolfSSL_SSL_get_secure_renegotiation_support: {e:?}"),
}
}

Expand All @@ -581,7 +581,7 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
match unsafe { wolfssl_sys::wolfSSL_SSL_renegotiate_pending(self.ssl.as_ptr()) } {
0 => false,
1 => true,
e => unreachable!("{e:?}"),
e => unreachable!("wolfSSL_SSL_renegotiate_pending: {e:?}"),
}
}

Expand Down Expand Up @@ -614,7 +614,7 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
}
e => Err(Error::fatal(e)),
},
e => unreachable!("{e:?}"),
e => Err(Error::fatal(e)),
}
}

Expand All @@ -640,10 +640,12 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
match unsafe { wolfssl_sys::wolfSSL_update_keys(self.ssl.as_ptr()) } {
wolfssl_sys::WOLFSSL_SUCCESS_c_int => Ok(Poll::Ready(())),
e @ wolfssl_sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG => unreachable!("{e:?}"),
e @ wolfssl_sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG => {
unreachable!("wolfSSL_update_keys: {e:?}")
}
wolfssl_sys::WOLFSSL_ERROR_WANT_WRITE_c_int => Ok(Poll::PendingWrite),

e => unreachable!("Received unknown code {e}"),
e => Err(Error::fatal(e)),
}
}

Expand Down Expand Up @@ -677,7 +679,7 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
0 => {}
// panic on non-success, because `ssl` is always non-null and the
// method here must be TLS1.3
e => unreachable!("{e:?}"),
e => unreachable!("wolfSSL_key_update_response: {e:?}"),
}

// SAFETY: Based on `wolfSSL_key_update_response`][0], required will be populated if the api returns success.
Expand All @@ -687,7 +689,7 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
match unsafe { required.assume_init() } {
1 => true,
0 => false,
e => unreachable!("{e:?}"),
e => unreachable!("wolfSSL_key_update_response: unexpected value: {e:?}"),
}
}

Expand All @@ -712,9 +714,11 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
// [1]: https://www.wolfssl.com/doxygen/ssl_8h.html#a07da5ada53a2a68ee8e7a6dab9b5f429
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
match unsafe { wolfssl_sys::wolfSSL_dtls_get_current_timeout(self.ssl.as_ptr()) } {
e @ wolfssl_sys::wolfCrypt_ErrorCodes_NOT_COMPILED_IN => unreachable!("{e:?}"),
e @ wolfssl_sys::wolfCrypt_ErrorCodes_NOT_COMPILED_IN => {
unreachable!("wolfSSL_dtls_get_current_timeout: {e:?}")
}
x if x > 0 => Duration::from_secs(x as u64),
e => unreachable!("{e:?}"),
e => unreachable!("wolfSSL_dtls_get_current_timeout: {e:?}"),
}
}

Expand Down Expand Up @@ -750,7 +754,7 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
} {
wolfssl_sys::WOLFSSL_SUCCESS_c_int => Ok(()),
x @ wolfssl_sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG => Err(Error::fatal(x)),
e => unreachable!("{e:?}"),
e => Err(Error::fatal(e)),
}
}

Expand Down Expand Up @@ -783,7 +787,7 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
} {
wolfssl_sys::WOLFSSL_SUCCESS_c_int => Ok(()),
x @ wolfssl_sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG => Err(Error::fatal(x)),
e => unreachable!("{e:?}"),
e => Err(Error::fatal(e)),
}
}

Expand All @@ -802,14 +806,16 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
// [1]: https://www.wolfssl.com/doxygen/ssl_8h.html#a86c630a78e966b768332c5b19e485a51
// [2]: https://www.wolfssl.com/documentation/manuals/wolfssl/chapter09.html#thread-safety
match unsafe { wolfssl_sys::wolfSSL_dtls_got_timeout(self.ssl.as_ptr()) } {
e @ wolfssl_sys::wolfCrypt_ErrorCodes_NOT_COMPILED_IN => unreachable!("{e:?}"),
e @ wolfssl_sys::wolfCrypt_ErrorCodes_NOT_COMPILED_IN => {
unreachable!("wolfSSL_dtls_got_timeout: {e:?}")
}
wolfssl_sys::WOLFSSL_SUCCESS_c_int => Poll::Ready(false),
x @ wolfssl_sys::wolfSSL_ErrorCodes_WOLFSSL_FATAL_ERROR => match self.get_error(x) {
wolfssl_sys::WOLFSSL_ERROR_WANT_READ_c_int => Poll::PendingRead,
wolfssl_sys::WOLFSSL_ERROR_WANT_WRITE_c_int => Poll::PendingWrite,
_ => Poll::Ready(true),
},
e => unreachable!("{e:?}"),
e => unreachable!("wolfSSL_dtls_got_timeout: {e:?}"),
}
}

Expand Down Expand Up @@ -854,7 +860,7 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
} {
wolfssl_sys::WOLFSSL_SUCCESS_c_int => Ok(()),
e @ wolfssl_sys::WOLFSSL_FAILURE_c_int => Err(Error::fatal(e)),
e => unreachable!("{e:?}"),
e => Err(Error::fatal(e)),
}
}

Expand Down Expand Up @@ -1066,7 +1072,7 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
// From implementation, the api expects valid pointer to `WOLFSSL`
match unsafe { wolfssl_sys::wolfSSL_dtls_set_mtu(self.ssl.as_ptr(), mtu) } {
wolfssl_sys::WOLFSSL_SUCCESS_c_int => {}
e => unreachable!("{e:?}"),
e => unreachable!("wolfSSL_dtls_set_mtu: {e:?}"),
}
}

Expand All @@ -1085,7 +1091,7 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
match unsafe { wolfssl_sys::wolfSSL_dtls(self.ssl.as_ptr()) } {
1 => true,
0 => false,
e => unreachable!("{e:?}"),
e => unreachable!("wolfSSL_dtls: {e:?}"),
}
}

Expand All @@ -1110,7 +1116,9 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
)
} {
wolfssl_sys::WOLFSSL_SUCCESS_c_int => Ok(()),
e @ wolfssl_sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG => unreachable!("{e:?}"),
e @ wolfssl_sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG => {
unreachable!("wolfSSL_UseSNI: {e:?}")
}
e => Err(Error::fatal(e)),
}
}
Expand All @@ -1135,7 +1143,7 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
} {
wolfssl_sys::WOLFSSL_SUCCESS_c_int => Ok(()),
x @ wolfssl_sys::WOLFSSL_FAILURE_c_int => Err(Error::fatal(self.get_error(x))),
e => unreachable!("{e:?}"),
e => Err(Error::fatal(e)),
}
}

Expand All @@ -1157,8 +1165,10 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
} {
wolfssl_sys::WOLFSSL_SUCCESS_c_int => Ok(()),
wolfssl_sys::wolfCrypt_ErrorCodes_MEMORY_E => panic!("Memory Allocation Failed"),
e @ wolfssl_sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG => unreachable!("{e:?}"),
e => unreachable!("{e:?}"),
e @ wolfssl_sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG => {
unreachable!("wolfSSL_UseKeyShare: {e:?}")
}
e => Err(Error::fatal(e)),
}
}

Expand Down Expand Up @@ -1204,7 +1214,7 @@ impl<IOCB: IOCallbacks> Session<IOCB> {
} {
wolfssl_sys::WOLFSSL_SUCCESS_c_int => Ok(()),
wolfssl_sys::wolfSSL_ErrorCodes_WOLFSSL_FATAL_ERROR => panic!("No SSL context"),
e => unreachable!("{e:?}"),
e => Err(Error::fatal(e)),
}
}

Expand Down

0 comments on commit ed93b38

Please sign in to comment.