Skip to main content

safe_read_bytes

Function safe_read_bytes 

Source
pub fn safe_read_bytes(
    buffer: &[u8],
    offset: usize,
    length: usize,
) -> Result<&[u8], IoError>
Expand description

Safely reads bytes from a buffer with bounds checking

This function provides safe access to buffer data with comprehensive bounds checking to prevent buffer overruns and invalid access patterns.

§Arguments

  • buffer - The buffer to read from
  • offset - Starting offset in the buffer
  • length - Number of bytes to read

§Returns

Returns a slice of the requested bytes on success, or an IoError if the access would be out of bounds.

§Errors

This function will return an error if:

  • The offset is beyond the buffer size
  • The length would cause an overflow
  • The offset + length exceeds the buffer size
  • The length is zero (invalid access)

§Examples

use libmagic_rs::io::safe_read_bytes;

let buffer = b"Hello, World!";
let result = safe_read_bytes(buffer, 0, 5)?;
assert_eq!(result, b"Hello");

let result = safe_read_bytes(buffer, 7, 6)?;
assert_eq!(result, b"World!");