Skip to content

slcan: drop malformed frame lines instead of raising - #2179

Open
mmporong wants to merge 1 commit into
hardbyte:mainfrom
mmporong:slcan-drop-malformed-frames
Open

mmporong wants to merge 1 commit into
hardbyte:mainfrom
mmporong:slcan-drop-malformed-frames

Conversation

@mmporong

@mmporong mmporong commented Dec 3, 2026

Copy link
Copy Markdown

Summary of Changes

  • slcanBus._recv_internal() now logs and drops a frame line that is cut short or contains invalid characters, instead of raising. A line with an odd number of data digits raised ValueError: non-hexadecimal number found in fromhex() arg at position N (the error in ValueError: non-hexadecimal number found in fromhex() arg at position 15 #1821), and an invalid identifier or DLC raised ValueError or IndexError. None of these is a CanError, and on a thread-based Notifier the first one ends the receive thread, so valid frames that follow are never delivered.
  • A line cut short after an even number of data digits was not detected at all: bytearray.fromhex() skips whitespace, so the \r terminator that fell into the data slice was ignored and the bus returned a message with dlc=8 and only 2 or 3 data bytes. The new _decode_data() helper checks that the decoded payload has the length the DLC asks for, for classic and FD frames.
  • Well-formed frames decode as before, including lines with trailing characters after the data (for example the 4 digit timestamp some adapters append). This follows the same log-and-drop approach udp_multicast: drop malformed datagrams instead of raising in _recv_internal #2174 took for UdpMulticastBus datagrams that do not decode; only IndexError and ValueError from the frame parsing are caught.

Related Issues / Pull Requests

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring
  • Other (please describe):

Checklist

  • I have followed the contribution guide.
  • I have added or updated tests as appropriate.
  • I have added or updated documentation as appropriate.
  • I have added a news fragment for towncrier.
  • All checks and tests pass (tox).

Additional Notes

Testing (Python 3.12, Linux container):

  • Reproduced ValueError: non-hexadecimal number found in fromhex() arg at position 15 #1821 on main: t1238112233445566778\r (15 of 16 data digits) raises ValueError: non-hexadecimal number found in fromhex() arg at position 15, and t12381\r raises at position 1. With a Notifier and a BufferedReader, a valid frame sent after such a line was never delivered: the receive thread ended and notifier.exception held the ValueError. On this branch the same sequence delivers the valid frame and the thread keeps running.
  • Fed 10 malformed lines and 8 well-formed lines (standard, standard with 4 trailing timestamp digits, extended, remote, extended remote, FD with DLC 9, FD BRS extended with DLC A, DLC 0) through slcanBus.recv() on main and on this branch. The 8 well-formed lines decode identically. The 10 malformed lines now return None with a warning, where main raised ValueError/IndexError for 7 of them and returned a message shorter than its DLC for the other 3 (dlc=8 with 3 and 2 data bytes, and an FD frame with dlc=64 and 5 data bytes).
  • The new test_recv_malformed_frame_is_dropped has 9 subtests that fail on main and pass here. pytest test/test_slcan.py: 21 passed, 9 subtests passed.
  • black --check, pylint can/interfaces/slcan.py (10.00/10) and mypy --python-version 3.12 can/interfaces/slcan.py pass. The full tox matrix was not run.

A frame line that is cut short or holds invalid characters made
_recv_internal() raise ValueError or IndexError from recv(), which ends
a thread-based Notifier (hardbyte#1821). A line cut short after an even number
of data digits decoded without error to fewer bytes than its DLC,
because bytearray.fromhex() skips the CR terminator that falls into the
data slice.

Check that the decoded payload has the length the DLC asks for, and log
and drop lines that do not parse, so reception continues with the next
frame. Well-formed lines, including ones with trailing characters after
the data, decode as before.
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.

ValueError: non-hexadecimal number found in fromhex() arg at position 15

1 participant