fix(proto): floor negative echo lengths

This commit is contained in:
Nils Andreas Svee committed 2026-08-12 17:16:03 +07:00
1 parent 2e2db4d3e0
commit 68ed5c6e2e
12 files changed
+107 -95

No files matched your search

+3 -3
View File
@@ -84,8 +84,8 @@ pub enum DecodedRequestKind<'a> {
/// # Errors
///
/// Returns [`ProtoError::PayloadTooLarge`] when an ECHO payload does not fit
/// the negotiated length, and [`ProtoError::NegativePacketLength`] when the
/// negotiated length is negative.
/// the negotiated length. A negative negotiated length is not an error: see
/// [`echo_packet_len`], which floors it at the mandatory field block.
pub fn encode_request(request: RequestToEncode<'_>, hmac_key: Option<&[u8]>) -> Result<Vec<u8>> {
match request {
RequestToEncode::Open { params, no_test } => encode_open(params, no_test, hmac_key),
@@ -205,7 +205,7 @@ fn encode_echo(
hmac_key: Option<&[u8]>,
) -> Result<Vec<u8>> {
let layout = PacketLayout::echo(hmac_key.is_some(), params);
let len = echo_packet_len(hmac_key.is_some(), params)?;
let len = echo_packet_len(hmac_key.is_some(), params);
let payload_offset = layout.header_len();
let available_payload_len = len.saturating_sub(payload_offset);
if payload.len() > available_payload_len {