Fix stack overflow when converting status enum back to u8

TODO: Add more test coverage!
This commit is contained in:
Nathan Fisher 2023-06-06 14:00:31 -04:00
parent bad5a230cc
commit ff96b5b56b
2 changed files with 20 additions and 4 deletions

View file

@ -70,7 +70,8 @@ where
let cfg = match client_cert {
None => cfg.with_no_client_auth(),
Some(c) => {
let rustls_cert = rustls::Certificate::read_bytes(&c.der)?;
//let rustls_cert = rustls::Certificate::read_bytes(&c.der)?;
let rustls_cert = rustls::Certificate(c.der);
let cert_chain = vec![rustls_cert];
let key_der = rustls::PrivateKey(c.key);
cfg.with_single_cert(cert_chain, key_der)?
@ -79,8 +80,8 @@ where
let client = ClientConnection::new(Arc::new(cfg), dnsname)?;
let mut stream = StreamOwned::new(client, tcp_stream);
stream.write_all(self.request.to_string().as_bytes())?;
let mut buf = vec![];
stream.read_to_end(&mut buf)?;
let mut buf = Vec::with_capacity(1024);
let _res = stream.read_to_end(&mut buf);
stream.conn.send_close_notify();
drop(stream);
let res = buf.try_into()?;

View file

@ -1,4 +1,6 @@
mod error;
use std::fmt;
pub use error::Error;
#[cfg(feature = "serde")]
@ -32,7 +34,8 @@ pub enum Status {
impl From<Status> for u8 {
fn from(value: Status) -> Self {
match value {
Status::Input | Status::Success => value.into(),
Status::Input => 10,
Status::Success => 20,
Status::Redirect(n) => 30 + n as u8,
Status::TemporaryFailure(n) => 40 + n as u8,
Status::PermanentFailure(n) => 50 + n as u8,
@ -200,6 +203,18 @@ impl TryFrom<u8> for AuthenticationFailure {
mod tests {
use super::*;
#[test]
fn to_number_success() {
let num: u8 = Status::Success.into();
assert_eq!(num, 20);
}
#[test]
fn to_number_redirect() {
let num: u8 = Status::Redirect(Redirect::Temporary).into();
assert_eq!(num, 30);
}
#[test]
fn parse_status_success() {
let status = Status::try_from(21).unwrap();