From 42ce8dbaeb46245b48ab10a0abfd44947496e00c Mon Sep 17 00:00:00 2001 From: TIM ANDERSON Date: Mon, 26 Oct 2026 15:22:47 +1000 Subject: [PATCH] fix(mf4): read packed scalar error-frame payloads --- can/io/mf4.py | 7 +-- doc/changelog.d/1913.fixed.rst | 2 + test/logformats_test.py | 78 +++++++++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 doc/changelog.d/1913.fixed.rst diff --git a/can/io/mf4.py b/can/io/mf4.py index fcde2e193..854842c92 100644 --- a/can/io/mf4.py +++ b/can/io/mf4.py @@ -415,9 +415,10 @@ def __iter__(self) -> Generator[Message, None, None]: and "CAN_ErrorFrame.DataBytes" in names ): data_length = int(data["CAN_ErrorFrame.DataLength"][i]) - kv["data"] = data["CAN_ErrorFrame.DataBytes"][i][ - :data_length - ].tobytes() + # A one-record slice preserves packed scalar byte order. + kv["data"] = data["CAN_ErrorFrame.DataBytes"][ + i : i + 1 + ].tobytes()[:data_length] yield Message(**kv) diff --git a/doc/changelog.d/1913.fixed.rst b/doc/changelog.d/1913.fixed.rst new file mode 100644 index 000000000..6aa03d8da --- /dev/null +++ b/doc/changelog.d/1913.fixed.rst @@ -0,0 +1,2 @@ +Fixed MF4 log reading crashing when CAN error-frame payloads are stored as packed +scalars instead of byte arrays. Payload length and byte order are preserved. diff --git a/test/logformats_test.py b/test/logformats_test.py index 42ed29b13..5a1cab94f 100644 --- a/test/logformats_test.py +++ b/test/logformats_test.py @@ -27,7 +27,7 @@ from parameterized import parameterized import can.io -from can.io import asc, blf +from can.io import asc, blf, mf4 from .data.example_data import ( TEST_COMMENTS, @@ -993,6 +993,82 @@ def _setup_instance(self): class TestMF4FileFormat(ReaderWriterTest): """Tests can.MF4Writer and can.MF4Reader""" + @parameterized.expand( + [ + ("reported_scalar", "u4", b"\x01\x02\x03\x04", 3, False), + ("empty_scalar", "