Skip to content

canutils: read the candump raw DLC suffix of Classical CAN frames - #2163

Merged
zariiii9003 merged 6 commits into
hardbyte:mainfrom
bIackr0se:fix/canutils-raw-dlc
Nov 28, 2026
Merged

zariiii9003 merged 6 commits into
hardbyte:mainfrom
bIackr0se:fix/canutils-raw-dlc

Conversation

@bIackr0se

@bIackr0se bIackr0se commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary of Changes

CanutilsLogReader raises ValueError: invalid literal for int() with base 16: '_C' on candump log lines such as 123#1122334455667788_C. The exception stops python -m can.logconvert, so the output omits that record and all later records. On remote lines, 123#R8_F raises too, and R8_0 to R8_9 read as DLC 80 to 89 because int("8_0") accepts the underscore as a digit separator.

ISO 11898-1 lets a Classical CAN frame with 8 data bytes carry a raw DLC value of 9 to 15. Linux SocketCAN reports this value in len8_dlc. When the interface reports one (the driver needs CAN_CTRLMODE_CC_LEN8_DLC), candump -L and candump -l write that raw DLC as a _<hex digit> suffix after 8 data bytes or R8 (can-utils lib.c, CC_DLC_DELIM '_', master 834ceef). can-utils keeps the payload length (8) in len and the raw DLC in the separate len8_dlc field.

This change makes CanutilsLogReader accept the suffix and read such a frame with dlc=8, within the 0 to 8 range that Message.dlc is documented to hold for Classical CAN. The raw DLC digit is dropped:

  • On a data line, a _ right after 8 Classical CAN data bytes ends the data; the rest is ignored.
  • On a remote line with a _, the reader takes the length as can-utils does: one digit from 0 to 8, else 0. 123#R8_F reads as DLC 8, 123#R08_C and 123#R9_C as DLC 0.
  • A _ after fewer than 8 data bytes (123#1122_C) and a suffix after 8 bytes of a CAN FD record (123##01122334455667788_C) still raise, as on main and as can-utils rejects them.
  • CanutilsLogWriter is unchanged.

Running python -m can.logconvert candump.log out.log shows the behavior before and after this change:

# Input: candump.log
(1764280800.000100) can0 100#0102030405060708
(1764280800.000200) can0 123#1122334455667788_C
(1764280800.000300) can0 200#R8_F
(1764280800.000400) can0 300#AABB

# Before (main): exit 1, traceback ending in ValueError: invalid literal for int() with base 16: '_C'
# out.log:
(1764280800.000100) can0 100#0102030405060708 R

# After: exit 0
# out.log:
(1764280800.000100) can0 100#0102030405060708 R
(1764280800.000200) can0 123#1122334455667788 R
(1764280800.000300) can0 200#R8 R
(1764280800.000400) can0 300#AABB R

Records without the suffix read as before.

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 (news fragment; no API change).
  • I have added a news fragment for towncrier.
  • All checks and tests pass (tox).

Additional Notes

parse_canframe from can-utils lib.c was compiled into a small program and fed 186 frame strings: standard and extended IDs, 0 to 8 data bytes, suffix digits 0 to F in upper and lower case, remote lines (R, R0 to R8, R8_0 to R8_F, R08_C, R9_C, R_C), CAN FD lines with flags 0 to 3, two error frames, trailing text after the digit, a _ after fewer than 8 bytes and a suffixed CAN FD record. Comparing the identifier, the remote and extended flags and the DLC (can-utils' len) of each record, CanutilsLogReader now matches can-utils on all 180 lines that can-utils accepts; on main it matched 72, raised on 88 and read the wrong DLC on 20. The 6 lines that can-utils rejects raise on main and after the change.

test/logformats_test.py adds test_read_raw_dlc (123#1122334455667788_C, 123#R8_F, 123#R08_C, 123#R9_C), which fails on main with ValueError, and test_read_raw_dlc_invalid (123#1122_C, 123##0R8_C, 123##01122334455667788_C), which passes on main and after the change.

tox was not run. These checks ran individually on macOS with Python 3.12.12: black --check . and ruff check can examples doc pass, pylint can/io scores 10.00/10, mypy --python-version 3.12 can reports no issues, and pytest test/logformats_test.py reports 131 passed and 21 skipped.

candump -8 -L writes the raw DLC (9 to 15) of a Classical CAN frame with
8 data bytes as a suffix: 123#1122334455667788_C, 123#R8_F.
CanutilsLogReader raised ValueError on such a line, which ended reading
of the whole file, and CanutilsLogWriter dropped the raw DLC.

Read the suffix into Message.dlc the way can-utils parse_canframe does
(only a digit from 9 to F after exactly 8 bytes), as the SocketCAN
interface already stores len8_dlc, and write it back in the same form.
A CAN FD record such as 123##0R8_C was read as a remote frame with
DLC 12; can-utils rejects it. Correct the candump option in the news
fragment and test comment: the suffix comes from candump -L and -l log
output, not -8.
…s does

can-utils reads one length digit from 0 to 8 after R and takes the raw
DLC only after the length 8, so 123#R08_C is a remote frame with DLC 0.
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-28T10:38:53.462073Z
We couldn’t complete this review. Comment @codex review to try again.
a9573a7 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@zariiii9003

Copy link
Copy Markdown
Collaborator

AI Review: canutils raw DLC (#2163)

The reader crash on _<dlc> suffixes is real and worth fixing. On main, 123#1122334455667788_C and 123#R8_F raise ValueError, and R8_0…R8_9 read as DLC 80–89. However, the PR stores the raw DLC in Message.dlc, which contradicts that attribute's definition.

Findings

  • can/io/canutils.py, _classic_dlc and the reader set Message.dlc to 9–15.

    • The python-can documentation (can.Message.dlc) defines dlc as "an integer between 0 and 8 representing the frame payload length" for Classical CAN, and Message._check() rejects larger values. Every suffixed record this PR reads fails that check.
    • can-utils (lib.c, parse_canframe) agrees: it keeps the payload length in len (8) and the raw DLC in the separate len8_dlc field.
    • The SocketCAN interface storing len8_dlc in dlc (CC-2174: modify CAN frame header structure to match updated struct ca… #1851) is a known deviation, not a model to follow. ASCReader already stores a classic raw DLC as min(8, dlc_code).
    • The deviation spreads to other formats: 123#1122334455667788_C converted to ASC is written as d c and read back as dlc=24, and TRC writes length 12 for 8 data bytes.

    Fix: keep the parsing, but discard the raw-DLC digit so the frame reads as dlc=8. For example, use if not is_fd and data[16:17] == "_": data = data[:16] on the data path, and keep your single-digit length handling on the remote path. Remove _classic_dlc, CAN_MAX_DLEN and CAN_MAX_RAW_DLC.

  • CanutilsLogWriter writes _<dlc> / R8_<dlc>. Once dlc keeps its documented range, a Classical CAN Message never has a dlc of 9–15, so this branch only encodes the deviation. Fix: revert the writer changes and remove test_write_raw_dlc.

  • test_read_raw_dlc. With dlc=8, the lines _9 (extended ID), _c, _8 and _CA no longer differ observably from _C. Fix: reduce the test to:

    • 123#1122334455667788_C → dlc=8 with 8 data bytes
    • 123#R8_F → dlc=8
    • 123#R08_C → dlc=0
    • 123#R9_C → dlc=0

    Keep the R9_C line: without it, a reader that accepts any remote length digit still passes.

  • test_read_raw_dlc_invalid. Removing not is_fd from the reader's data path leaves every test green, so 123##01122334455667788_C would be accepted although can-utils rejects it. Fix: add ("123##01122334455667788_C",).

  • test_remote_frame_dlc. The added (1, "123#R1") case passes on main and duplicates (5, "123#R5"). Fix: remove it.

  • doc/changelog.d/2163.fixed.rst. Fix: reword it to match the behaviour after the fix, e.g. "Fixed CanutilsLogReader stopping with a ValueError on candump log lines that carry a Classical CAN raw DLC (123#1122334455667788_C, 123#R8_F) and reading R8_0 to R8_9 as DLC 80 to 89. Such frames are now read with dlc=8."

Verdict

request changes

Message.dlc holds the payload length of a Classical CAN frame (0 to 8),
and can-utils keeps the raw DLC in the separate len8_dlc field. The
reader now drops the raw DLC digit after 8 data bytes or R8, and the
writer is back to main's behaviour.
@bIackr0se bIackr0se changed the title canutils: read and write the candump raw DLC of Classical CAN frames canutils: read the candump raw DLC suffix of Classical CAN frames Nov 28, 2026
@bIackr0se

Copy link
Copy Markdown
Contributor Author

Thanks, all six points are in 09c89a5:

  • The reader drops the raw DLC digit, so 123#1122334455667788_C and 123#R8_F read with dlc=8. _classic_dlc, CAN_MAX_DLEN and CAN_MAX_RAW_DLC are gone.
  • The writer is back to main, and test_write_raw_dlc is removed.
  • test_read_raw_dlc has only the four lines you listed (_C, R8_F, R08_C, R9_C) and fails on main with ValueError.
  • test_read_raw_dlc_invalid has the 123##01122334455667788_C case. Removing not is_fd from either the data or the remote path now fails the canutils tests.
  • The (1, "123#R1") case is removed, and the news fragment uses your wording.

Against can-utils' parse_canframe over the same 186 lines, the reader now matches len on all 180 lines can-utils accepts. python -m can.logconvert on a log with _C and R8_F lines exits 0, and converting it to ASC and back gives dlc=8 for both. The PR description is updated to match.

@zariiii9003 zariiii9003 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@zariiii9003
zariiii9003 merged commit 6ecdddd into hardbyte:main Nov 28, 2026
28 checks 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.

2 participants