Fix crash in server due to overeager parsing
All checks were successful
Build library & run tests / build (unix) (push) Successful in 29s
Build library & run tests / build (tcp) (push) Successful in 29s

This commit is contained in:
Kodi Craft 2024-06-24 15:58:54 +02:00
parent fc570fa0bd
commit bf183a0598
Signed by: kodi
GPG Key ID: 69D9EED60B242822
2 changed files with 29 additions and 16 deletions

View File

@ -320,7 +320,7 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
Ok(n) => { Ok(n) => {
#debug("Received {} bytes (server)", n); #debug("Received {} bytes (server)", n);
buf.extend_from_slice(&read_buf[..n]); buf.extend_from_slice(&read_buf[..n]);
loop { while buf.len() >= 4 {
let len = u32::from_le_bytes(buf[..4].try_into().expect("Failed to convert bytes to u32")); let len = u32::from_le_bytes(buf[..4].try_into().expect("Failed to convert bytes to u32"));
if buf.len() >= (4 + len as usize) { if buf.len() >= (4 + len as usize) {
let serialized = std::str::from_utf8(&buf[4..(4 + len as usize)]).expect("Failed to convert bytes to string"); let serialized = std::str::from_utf8(&buf[4..(4 + len as usize)]).expect("Failed to convert bytes to string");

View File

@ -53,29 +53,42 @@ impl TestProtocolServerTrait for TrivialServer {
async fn void(&mut self) {} async fn void(&mut self) {}
} }
struct Cleanup {
address: String,
}
impl Drop for Cleanup {
fn drop(&mut self) {
std::fs::remove_file(&self.address).unwrap();
}
}
#[tokio::test] #[tokio::test]
async fn e2e() { async fn e2e() {
init_logger(); init_logger();
#[cfg(feature = "unix")] #[cfg(feature = "unix")]
let address = "/tmp/eagle-test.sock"; let address = format!("/tmp/eagle-test-{}.sock", rand::random::<u64>());
#[cfg(feature = "unix")]
let _cleanup = Cleanup {
address: address.clone(),
};
#[cfg(feature = "tcp")] #[cfg(feature = "tcp")]
let address = format!("127.0.0.1:{}", 10000 + rand::random::<u64>() % 1000); let address = format!("127.0.0.1:{}", 10000 + rand::random::<u64>() % 1000);
let server_task = tokio::spawn(TestProtocolServer::bind(TrivialServer, address.clone())); let server_task = tokio::spawn(TestProtocolServer::bind(TrivialServer, address.clone()));
// Wait for the server to start // Wait for the server to start
tokio::time::sleep(std::time::Duration::from_millis(10)).await; tokio::time::sleep(std::time::Duration::from_millis(10)).await;
let client = TestProtocolClient::connect(address).await.unwrap(); let client = TestProtocolClient::connect(address).await.unwrap();
let res = client.addition(2, 5).await.unwrap(); assert_eq!(client.addition(2, 5).await.unwrap(), 7);
// assert_eq!(client.addition(2, 5).await.unwrap(), 7); assert_eq!(
// assert_eq!( client
// client.some_kind_of_question("Hello, world!".to_string()) .some_kind_of_question("Hello, world!".to_string())
// .await .await
// .unwrap(), .unwrap(),
// "Hello, world!".len() as i32 "Hello, world!".len() as i32
// ); );
// assert_eq!( assert_eq!(
// client.this_responds_with_a_string(42).await.unwrap(), client.this_responds_with_a_string(42).await.unwrap(),
// "The number is 42" "The number is 42"
// ); );
// client.void().await.unwrap(); client.void().await.unwrap();
// server_task.abort(); server_task.abort();
} }