From 2cea27d1bcc2ea362acdc9c8efb8b1630e5da696 Mon Sep 17 00:00:00 2001 From: Eldad Sitbon Date: Thu, 5 Sep 2024 09:29:27 +0000 Subject: [PATCH] allow only valid fd message lengths --- can/interfaces/socketcan/socketcan.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/can/interfaces/socketcan/socketcan.py b/can/interfaces/socketcan/socketcan.py index 6640ea62b..32fcff549 100644 --- a/can/interfaces/socketcan/socketcan.py +++ b/can/interfaces/socketcan/socketcan.py @@ -238,7 +238,8 @@ def build_can_frame(msg: Message) -> bytes: data = bytes(msg.data).ljust(max_len, b"\x00") - return CAN_FRAME_HEADER_STRUCT.pack(can_id, len(msg.data), flags, msg.dlc) + data + data_len = min(i for i in can.util.CAN_FD_DLC if i >= len(msg.data)) + return CAN_FRAME_HEADER_STRUCT.pack(can_id, data_len, flags, msg.dlc) + data def build_bcm_header( @@ -321,6 +322,9 @@ def dissect_can_frame(frame: bytes) -> Tuple[int, int, int, bytes]: # Flags not valid in non-FD frames flags = 0 + if data_len not in can.util.CAN_FD_DLC: + data_len = min(i for i in can.util.CAN_FD_DLC if i >= data_len) + # Allow deprecated can frames with old struct if ( data_len == constants.CAN_MAX_DLEN and