Skip to content

serial: raise CanOperationError on truncated frame reads - #2175

Open
aloneangel45 wants to merge 2 commits into
hardbyte:mainfrom
aloneangel45:fix/serial-truncated-read-error
Open

aloneangel45 wants to merge 2 commits into
hardbyte:mainfrom
aloneangel45:fix/serial-truncated-read-error

Conversation

@aloneangel45

Copy link
Copy Markdown

Summary of Changes

  • Add SerialBus._read_exact() helper in can/interfaces/serial/serial_can.py to verify each post-SOF frame read ( imestamp, dlc, �rbitration_id, payload data when non-RTR and dlc > 0, and trailing xBB delimiter) returns the expected byte count, raising CanOperationError instead of propagating struct.error or TypeError when a serial read times out mid-frame.
  • Preserve existing behavior for idle timeouts and non-xAA start bytes (returns None), dlc > 8 (ValueError), and non-xBB delimiters (CanOperationError).
  • Add regression tests in est/serial_test.py (SimpleSerialLoopTest using pyserial's loop:// URL) covering truncated frames across every field boundary, idle/non-SOF reads, invalid delimiters, and invalid DLCs.

Related Issues / Pull Requests

Type of Change

  • Bug fix

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • All checks and tests pass (pytest -o addopts="" -v test/serial_test.py -> 34 passed, 7 subtests passed; �lack --check .;
    uff check can examples doc; mypy --python-version 3.11 & 3.14 can/interfaces/serial/serial_can.py).

@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review: serial: raise CanOperationError on truncated frame reads

The fix resolves #2157: every truncation point now raises CanOperationError, and well-formed, zero-length and remote frames still decode. It also stops a frame whose payload read timed out from being returned with dlc=8 but only 2 data bytes when a late 0xBB arrives.

Findings

  • Missing news fragment (doc/changelog.d/): the contribution guide requires one for every bugfix. Add e.g. 2175.fixed.rst:
    The ``serial`` interface raises :class:~can.exceptions.CanOperationError when a read times out in the middle of a frame, instead of ``struct.error`` or ``TypeError``, or a message with a truncated payload.
  • Redundant condition (serial_can.py, data = b"" if is_remote_frame or dlc == 0 else ...): _read_exact(0) already returns b"", so or dlc == 0 has no effect. Remove it.
  • Payload check is untested (test_truncated_frame_raises_can_operation_error, cut=12): if the payload read is reverted to self._ser.read(dlc), all tests stay green, because the delimiter read raises afterwards. Add a test where the payload read returns too few bytes and a 0xBB follows, e.g. in SimpleSerialTest:
    self.mock_serial.return_value.read = Mock(side_effect=[
        b"\xaa", bytes(4), b"\x08", struct.pack("<I", 0x123), b"\x01\x02", b"\xbb"])
    with self.assertRaises(can.CanOperationError):
        self.bus.recv(timeout=0)
  • Duplicate cut points (test_truncated_frame_raises_can_operation_error): cuts 1, 6 and 12 detect no regression that cuts 3, 8 and the delimiter cut do not already detect, and each cut waits for the serial timeout. Reduce the cuts to (3, 5, 8, len(frame) - 1).
  • Redundant test block (the remote-frame part of test_truncated_frame_raises_can_operation_error): it only repeats the missing-delimiter case (cut=len(frame) - 1). Remote-frame handling is already guarded by test_rx_tx_rtr_frame. Remove the block.
  • Tests outside the PR's scope (test_empty_and_non_start_byte_return_none, test_invalid_delimiter_raises_can_operation_error, test_invalid_dlc_raises_value_error): they cover code paths whose behaviour this PR does not change, and none of them fails when a statement changed by this PR is broken. test_invalid_dlc_raises_value_error also pins the ValueError that serial: a frame cut off by the read timeout raises struct.error / TypeError instead of CanOperationError #2157 reports as a defect. Remove all three.

Verdict

request changes

@aloneangel45

Copy link
Copy Markdown
Author

@zariiii9003 Addressed every item from your review in 8fc8fea:

  1. News fragment: Added doc/changelog.d/2175.fixed.rst.
  2. Removed redundant condition: Simplified serial_can.py back to data = b"" if is_remote_frame else self._read_exact(dlc) since _read_exact(0) already returns b"".
  3. Payload short-read coverage: Added SimpleSerialTest.test_short_payload_with_trailing_delimiter_raises_can_operation_error with mocked serial reads returning 2 payload bytes followed by b"\xbb" when dlc=8.
  4. Tightened loop tests: Reduced truncation cut points in test_truncated_frame_raises_can_operation_error to (3, 5, 8, len(frame) - 1), removed the redundant RTR truncation block, and removed the three out-of-scope tests.

Verified locally with pytest -o addopts= test/serial_test.py -v (32 passed, 4 subtests passed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

serial: a frame cut off by the read timeout raises struct.error / TypeError instead of CanOperationError

2 participants