Skip to content

The example of function set_bits_from_byte has a write out-of-bounds error. #23

@rikkaii

Description

@rikkaii

Hi, I've noticed that the following code example causes a write out-of-bounds error. The ffi::modbus_set_bits_from_byte function always writes 8 bytes, and a write out-of-bounds error occurs when the length of dest is less than index + 8.

libmodbus-rs/src/modbus.rs

Lines 831 to 849 in 979e99c

/// # Examples
///
/// ```rust
/// use libmodbus::{Modbus, ModbusMapping, ModbusTCP};
/// use libmodbus::prelude::*;
/// let modbus = Modbus::new_tcp("127.0.0.1", 1502).unwrap();
/// let modbus_mapping = ModbusMapping::new(5, 5, 5, 5).unwrap();
///
/// // before
/// assert_eq!(modbus_mapping.get_input_bits_mut(), [0u8, 0, 0, 0, 0]);
///
/// set_bits_from_byte(modbus_mapping.get_input_bits_mut(), 2, 0b1111_1111);
///
/// // after
/// assert_eq!(modbus_mapping.get_input_bits_mut(), [0u8, 0, 1, 1, 1]);
/// ```
pub fn set_bits_from_byte(dest: &mut [u8], index: u32, value: u8) {
unsafe { ffi::modbus_set_bits_from_byte(dest.as_mut_ptr(), index as c_int, value) }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions