krishvishal opened a new issue, #2878:
URL: https://github.com/apache/iggy/issues/2878

   ### Bug description
   
   `bytemuck::Pod` requires that every possible bit pattern is a valid instance 
of the type.
   
     - `Command2` defines discriminants `0..=12` only (13 of 256 possible u8 
values)
     - `Operation` defines discriminants `0, 128..=147, 160..=161, 200` (23 of 
256 possible u8 values)
   
   When `bytemuck::from_bytes::<PrepareHeader>(&buf)` is called on a network 
buffer containing e.g. command = `0xFF`, Rust materializes a Command2 with an 
invalid discriminant. This is UB and the compiler is free to assume enum fields 
always hold valid discriminants. 
   
   ### Deployment
   
   Not applicable
   
   ### Versions
   
   _No response_
   
   ### Hardware / environment
   
   _No response_
   
   ### Sample code
   
   ```rust
   use bytemuck::{Pod, Zeroable};
   
   /// Only discriminants 0, 1, 2 are valid.
   #[derive(Debug, Clone, Copy, PartialEq, Eq)]
   #[repr(u8)]
   enum Command {
       Ping = 0,
       Pong = 1,
       Request = 2,
   }
   
   #[derive(Debug, Clone, Copy)]
   #[repr(C)]
   struct Header {
       command: Command,
   }
   
   unsafe impl Zeroable for Header {}
   unsafe impl Pod for Header {}
   
   fn main() {
       let buf: [u8; 1] = [255];
   
       let header: &Header = bytemuck::from_bytes(&buf);
   
       match header.command {
           Command::Ping => println!("ping"),
           Command::Pong => println!("pong"),
           Command::Request => println!("request"),
       }
   
       println!("command == Ping? {}", header.command == Command::Ping);
       println!("raw byte: 0x{:02X}", header.command as u8);
   }
   ```
   
   ### Logs
   
   _No response_
   
   ### Iggy server config
   
   _No response_
   
   ### Reproduction
   
   Try this MWE on Rust Playground: 
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=2e4f80fe9eb435682b0ede6b7c3c95b8
   
   And run the above example using Miri to see it throw error. 


-- 
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