Skip to content

Commit e80759b

Browse files
committed
refactor: add debug_assert to error_message()
1 parent 810a25b commit e80759b

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

crates/net/p2p/src/req_resp/messages.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,24 @@ pub type RequestedBlockRoots = ssz_types::VariableList<H256, MaxRequestBlocks>;
110110
pub type ErrorMessage = ssz_types::VariableList<u8, MaxErrorMessageLength>;
111111

112112
/// Helper to create an ErrorMessage from a string.
113-
/// Truncates to 256 bytes if necessary.
113+
/// Debug builds panic if message exceeds 256 bytes (programming error).
114+
/// Release builds truncate to 256 bytes.
114115
pub fn error_message(msg: impl AsRef<str>) -> ErrorMessage {
115116
let bytes = msg.as_ref().as_bytes();
117+
debug_assert!(
118+
bytes.len() <= 256,
119+
"Error message exceeds 256 byte protocol limit: {} bytes. Message: '{}'",
120+
bytes.len(),
121+
msg.as_ref()
122+
);
123+
116124
let truncated = if bytes.len() > 256 {
117125
&bytes[..256]
118126
} else {
119127
bytes
120128
};
121129

122-
let vec = truncated.to_vec();
123-
ErrorMessage::new(vec).expect("error message fits in 256 bytes")
130+
ErrorMessage::new(truncated.to_vec()).expect("error message fits in 256 bytes")
124131
}
125132

126133
#[derive(Debug, Clone, Encode, Decode)]

0 commit comments

Comments
 (0)