From 29d6d0a90664702ded201a51e6710247d187d147 Mon Sep 17 00:00:00 2001 From: gazoodle <47351872+gazoodle@users.noreply.github.com> Date: Fri, 17 Jun 2022 02:35:25 +0100 Subject: [PATCH] Fix modbus user-defined function handling (#3527) --- esphome/components/modbus/modbus.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/esphome/components/modbus/modbus.cpp b/esphome/components/modbus/modbus.cpp index 19b5e8019e..0928b0f6f3 100644 --- a/esphome/components/modbus/modbus.cpp +++ b/esphome/components/modbus/modbus.cpp @@ -76,7 +76,12 @@ bool Modbus::parse_modbus_byte_(uint8_t byte) { // installed, but wait, there is the CRC, and if we get a hit there is a good // chance that this is a complete message ... admittedly there is a small chance is // isn't but that is quite small given the purpose of the CRC in the first place - data_len = at; + + // Fewer than 2 bytes can't calc CRC + if (at < 2) + return true; + + data_len = at - 2; data_offset = 1; uint16_t computed_crc = crc16(raw, data_offset + data_len);