Skip to content

Commit 4b5744b

Browse files
committed
plugin(modbus): fix invalid message check
1 parent bd184ae commit 4b5744b

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

plugins/modbus/modbus_stack.c

+28-10
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,36 @@ int modbus_stack_recv(modbus_stack_t *stack, uint8_t slave_id,
100100
return -1;
101101
}
102102

103-
if (stack->protocol == MODBUS_PROTOCOL_TCP && data.n_byte == 0xff) {
104-
bytes = neu_protocol_unpack_buf(buf,
105-
header.len -
106-
sizeof(struct modbus_code) -
107-
sizeof(struct modbus_data));
108-
stack->value_fn(stack->ctx, code.slave_id,
109-
header.len - sizeof(struct modbus_code) -
110-
sizeof(struct modbus_data),
111-
bytes, 0);
112-
} else {
103+
switch (stack->protocol) {
104+
case MODBUS_PROTOCOL_TCP:
105+
if (data.n_byte == 0xff) {
106+
bytes = neu_protocol_unpack_buf(buf,
107+
header.len -
108+
sizeof(struct modbus_code) -
109+
sizeof(struct modbus_data));
110+
if (bytes == NULL) {
111+
return -1;
112+
}
113+
stack->value_fn(stack->ctx, code.slave_id,
114+
header.len - sizeof(struct modbus_code) -
115+
sizeof(struct modbus_data),
116+
bytes, 0);
117+
} else {
118+
bytes = neu_protocol_unpack_buf(buf, data.n_byte);
119+
if (bytes == NULL) {
120+
return -1;
121+
}
122+
stack->value_fn(stack->ctx, code.slave_id, data.n_byte, bytes,
123+
0);
124+
}
125+
break;
126+
case MODBUS_PROTOCOL_RTU:
113127
bytes = neu_protocol_unpack_buf(buf, data.n_byte);
128+
if (bytes == NULL) {
129+
return -1;
130+
}
114131
stack->value_fn(stack->ctx, code.slave_id, data.n_byte, bytes, 0);
132+
break;
115133
}
116134

117135
break;

0 commit comments

Comments
 (0)