Skip to main content

validate_buffer_access

Function validate_buffer_access 

Source
pub fn validate_buffer_access(
    buffer_size: usize,
    offset: usize,
    length: usize,
) -> Result<(), IoError>
Expand description

Validates buffer access parameters without performing the actual read

This function can be used to validate buffer access parameters before performing the actual read operation.

§Arguments

  • buffer_size - Size of the buffer
  • offset - Starting offset
  • length - Number of bytes to access

§Returns

Returns Ok(()) if the access is valid, or an IoError if it 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::validate_buffer_access;

// Valid access
validate_buffer_access(100, 10, 20)?;

// Invalid access - would go beyond buffer
let result = validate_buffer_access(100, 90, 20);
assert!(result.is_err());