Guard max size of msg

This commit is contained in:
2025-10-30 19:54:05 +01:00
parent bd8be1a6c4
commit 41129f475d
2 changed files with 8 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ pub enum Error {
Json(miniserde::Error),
Framing,
Protocol(String),
MessageTooLarge(usize),
}
pub type Result<T> = std::result::Result<T, Error>;
@@ -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"),
}
}
}

View File

@@ -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<usize> {
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)?;