diff --git a/crates/oc2r-core/src/error.rs b/crates/oc2r-core/src/error.rs index 3c50e54..d1f04e8 100644 --- a/crates/oc2r-core/src/error.rs +++ b/crates/oc2r-core/src/error.rs @@ -8,6 +8,7 @@ pub enum Error { Json(miniserde::Error), Framing, Protocol(String), + MessageTooLarge(usize), } pub type Result = std::result::Result; @@ -31,6 +32,7 @@ impl std::fmt::Display for Error { Error::Json(e) => write!(f, "json error: {e}"), Error::Framing => f.write_str("invalid message framing"), Error::Protocol(msg) => write!(f, "protocol error: {msg}"), + Error::MessageTooLarge(n) => write!(f, "rpc frame too large: {n} bytes"), } } } diff --git a/crates/oc2r-core/src/transport.rs b/crates/oc2r-core/src/transport.rs index e849efd..d4c97ba 100644 --- a/crates/oc2r-core/src/transport.rs +++ b/crates/oc2r-core/src/transport.rs @@ -9,6 +9,7 @@ use std::os::fd::RawFd; use std::time::{Duration, Instant}; const DELIM: [u8; 1] = [0]; +const MAX_MESSAGE_SIZE: usize = 4096; pub(crate) fn set_raw(fd: RawFd) -> Result<()> { unsafe { @@ -74,6 +75,11 @@ pub(crate) fn flush_fd(fd: RawFd) -> Result { pub(crate) fn write_message(fd: RawFd, msg: &impl Serialize) -> Result<()> { let payload = json::to_string(msg); + + let total_len = payload.len() + 2; + if total_len > MAX_MESSAGE_SIZE { + return Err(Error::MessageTooLarge(total_len)); + } write_all_fd(fd, &DELIM)?; write_all_fd(fd, payload.as_bytes())?; write_all_fd(fd, &DELIM)?;