krishvishal commented on code in PR #3284:
URL: https://github.com/apache/iggy/pull/3284#discussion_r3273439129


##########
core/common/src/types/send_messages2.rs:
##########
@@ -753,3 +774,49 @@ fn read_u128(bytes: &[u8], offset: usize) -> Result<u128, 
IggyError> {
         .map(u128::from_le_bytes)
         .ok_or(IggyError::InvalidNumberEncoding)
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use iggy_binary_protocol::Command2;
+
+    fn aligned_prepare_bytes(size: u32) -> Owned<MESSAGE_ALIGN> {
+        let mut owned = 
Owned::<MESSAGE_ALIGN>::zeroed(std::mem::size_of::<PrepareHeader>());
+        let header: &mut PrepareHeader =
+            bytemuck::checked::try_from_bytes_mut(owned.as_mut_slice())
+                .expect("zeroed bytes form a valid PrepareHeader");
+        header.command = Command2::Prepare;
+        header.size = size;
+        owned
+    }
+
+    #[test]
+    fn decode_prepare_slice_size_below_header_size_does_not_panic() {
+        // Regression: without the `total_size < header_size` guard,
+        // `&bytes[256..size]` panics for any size < 256.
+        for adversarial_size in [0u32, 255] {
+            let owned = aligned_prepare_bytes(adversarial_size);
+            let result = decode_prepare_slice(owned.as_slice());
+            assert!(
+                matches!(result, Err(IggyError::InvalidCommand)),
+                "size={adversarial_size} must be rejected, got {result:?}",
+            );
+        }
+    }
+
+    #[cfg(debug_assertions)]
+    #[test]
+    #[should_panic(expected = "must be at least 16-byte aligned")]
+    fn decode_prepare_slice_debug_asserts_on_misaligned_input() {
+        // `Vec<u8>` requests align=1; glibc returns 16-aligned bases so
+        // `&buf[1..]` is reliably misaligned (16k + 1 mod 16 = 1).
+        let buf: Vec<u8> = vec![0u8; std::mem::size_of::<PrepareHeader>() + 1];

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to