Skip to content

RUST-2233 Cherrypick more fixes for 3.2.4 release #1407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ rayon = { version = "1.5.3", optional = true }
rustc_version_runtime = "0.3.0"
rustls-pemfile = { version = "1.0.1", optional = true }
serde_with = "3.8.1"
sha-1 = "0.10.0"
sha1 = "0.10.0"
sha2 = "0.10.2"
snap = { version = "1.0.5", optional = true }
socket2 = "0.5.5"
Expand Down
27 changes: 17 additions & 10 deletions src/cmap/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ impl Connection {

self.command_executing = true;

let max_message_size = self.max_message_size_bytes();
#[cfg(any(
feature = "zstd-compression",
feature = "zlib-compression",
Expand All @@ -230,30 +231,30 @@ impl Connection {
let write_result = match self.compressor {
Some(ref compressor) if message.should_compress => {
message
.write_op_compressed_to(&mut self.stream, compressor)
.write_op_compressed_to(&mut self.stream, compressor, max_message_size)
.await
}
_ => {
message
.write_op_msg_to(&mut self.stream, max_message_size)
.await
}
_ => message.write_op_msg_to(&mut self.stream).await,
};
#[cfg(all(
not(feature = "zstd-compression"),
not(feature = "zlib-compression"),
not(feature = "snappy-compression")
))]
let write_result = message.write_op_msg_to(&mut self.stream).await;
let write_result = message
.write_op_msg_to(&mut self.stream, max_message_size)
.await;

if let Err(ref err) = write_result {
self.error = Some(err.clone());
}
write_result?;

let response_message_result = Message::read_from(
&mut self.stream,
self.stream_description
.as_ref()
.map(|d| d.max_message_size_bytes),
)
.await;
let response_message_result = Message::read_from(&mut self.stream, max_message_size).await;
self.command_executing = false;
if let Err(ref err) = response_message_result {
self.error = Some(err.clone());
Expand Down Expand Up @@ -306,6 +307,12 @@ impl Connection {
pub(crate) fn is_streaming(&self) -> bool {
self.more_to_come
}

fn max_message_size_bytes(&self) -> Option<i32> {
self.stream_description
.as_ref()
.map(|d| d.max_message_size_bytes)
}
}

/// A handle to a pinned connection - the connection itself can be retrieved or returned to the
Expand Down
20 changes: 20 additions & 0 deletions src/cmap/conn/wire/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ impl Message {
pub(crate) async fn write_op_msg_to<T: AsyncWrite + Send + Unpin>(
&self,
mut writer: T,
max_message_size_bytes: Option<i32>,
) -> Result<()> {
let sections = self.get_sections_bytes()?;

Expand All @@ -286,6 +287,15 @@ impl Message {
.map(std::mem::size_of_val)
.unwrap_or(0);

let max_len =
Checked::try_from(max_message_size_bytes.unwrap_or(DEFAULT_MAX_MESSAGE_SIZE_BYTES))?;
if total_length > max_len {
return Err(ErrorKind::InvalidArgument {
message: format!("Message length {} over maximum {}", total_length, max_len),
}
.into());
}

let header = Header {
length: total_length.try_into()?,
request_id: self.request_id.unwrap_or_else(next_request_id),
Expand Down Expand Up @@ -316,6 +326,7 @@ impl Message {
&self,
mut writer: T,
compressor: &Compressor,
max_message_size_bytes: Option<i32>,
) -> Result<()> {
let flag_bytes = &self.flags.bits().to_le_bytes();
let section_bytes = self.get_sections_bytes()?;
Expand All @@ -329,6 +340,15 @@ impl Message {
+ std::mem::size_of::<u8>()
+ compressed_bytes.len();

let max_len =
Checked::try_from(max_message_size_bytes.unwrap_or(DEFAULT_MAX_MESSAGE_SIZE_BYTES))?;
if total_length > max_len {
return Err(ErrorKind::InvalidArgument {
message: format!("Message length {} over maximum {}", total_length, max_len),
}
.into());
}

let header = Header {
length: total_length.try_into()?,
request_id: self.request_id.unwrap_or_else(next_request_id),
Expand Down