mirror of
https://github.com/esphome/esphome.git
synced 2026-09-19 11:08:38 +00:00
[modbus_controller] Fix off-by-one bounds check in byte_from_hex_str (#15301)
This commit is contained in:
@@ -81,15 +81,15 @@ inline ModbusFunctionCode modbus_register_write_function(ModbusRegisterType reg_
|
||||
inline uint8_t c_to_hex(char c) { return (c >= 'A') ? (c >= 'a') ? (c - 'a' + 10) : (c - 'A' + 10) : (c - '0'); }
|
||||
|
||||
/** Get a byte from a hex string
|
||||
* hex_byte_from_str("1122",1) returns uint_8 value 0x22 == 34
|
||||
* hex_byte_from_str("1122",0) returns 0x11
|
||||
* byte_from_hex_str("1122", 1) returns uint_8 value 0x22 == 34
|
||||
* byte_from_hex_str("1122", 0) returns 0x11
|
||||
* @param value string containing hex encoding
|
||||
* @param position offset in bytes. Because each byte is encoded in 2 hex digits the position of the original byte in
|
||||
* the hex string is byte_pos * 2
|
||||
* @return byte value
|
||||
*/
|
||||
inline uint8_t byte_from_hex_str(const std::string &value, uint8_t pos) {
|
||||
if (value.length() < pos * 2 + 1)
|
||||
if (value.length() < pos * 2 + 2)
|
||||
return 0;
|
||||
return (c_to_hex(value[pos * 2]) << 4) | c_to_hex(value[pos * 2 + 1]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user