Skip to content

fix: don't modify msg.data when padding frame for GsUsbFrame - #2085

Open
bjarki-andreasen wants to merge 1 commit into
hardbyte:mainfrom
bjarki-andreasen:copy-msg-data-on-send
Open

fix: don't modify msg.data when padding frame for GsUsbFrame#2085
bjarki-andreasen wants to merge 1 commit into
hardbyte:mainfrom
bjarki-andreasen:copy-msg-data-on-send

Conversation

@bjarki-andreasen

@bjarki-andreasen bjarki-andreasen commented Aug 3, 2026

Copy link
Copy Markdown

The current implementation of GsUsbBus send() modifies the msg.data bytearray in place, when padding it up to CAN_MAX_DLC for a GsUsbFrame. This is an issue as the can.Message equals method compares the data fields of messages, so if GsUsbBus send() modifies the data of a can.Message with dlc == 0, the padded data does not match the original message:

The passed can.Message should not be modified, only read. This commit creates a new padded bytearray from the msg.data bytearray, leaving the passed can.Message intact.

Summary of Changes

  • Copy msg.data and pad the copy, rather than modifying the msg.data member in place

Related Issues / Pull Requests

  • Closes #
  • Related to #

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).

Note that tox run showed 47 failed tests when run locally, unrelated to changes in this PR (mostly interfaces like pcan and kvaser). The added test passed.

Additional Notes

Issue was discovered while running this zephyr-rtos test suite https://github.com/zephyrproject-rtos/zephyr/tree/main/tests/drivers/can/host using a cannectivity based probe, which implements the gs_usb interface. The following error was observed:

2026-08-02 21:46:11,310 - twister - DEBUG - PYTEST: >       self.check_rx(msg, rx)
2026-08-02 21:46:11,310 - twister - DEBUG - PYTEST: E       Failed: rx message "Timestamp:        0.000000    ID:      010    S Rx                DL:  0                                Channel: scan@4200" not equal to tx message "Timestamp:        0.000000    ID:      010    S Rx                DL:  0"
2026-08-02 21:46:11,310 - twister - DEBUG - PYTEST: zephyr/tests/drivers/can/host/pytest/test_can.py:98: Failed

which was quite puzling given it "looks" identical because the data field is not printed if DLC == 0. debugging showed the mismatching data members, which failed the can.Message equals check, as this one checks data == data even when DLC == 0

There is no explicit documentation stating that the can.Message should not be modified, it can only be inferred from what I can see, maybe we should document this if it is indeed expected to not mutate the can.Message objects post creation? A small step in that direction could be to make the data a bytes object rather than bytearray, then gs_usb.py could not have called .extend() in the first place.

The current implementation of GsUsbBus send() modifies the msg.data
bytearray in place, when padding it up to CAN_MAX_DLC for a
GsUsbFrame. This is an issue as the can.Message equals method
compares the data fields of messages, so if GsUsbBus send() modifies
the data of a can.Message with dlc == 0, the padded data does not
match the original message:

The passed can.Message should not be modified, only read. This commit
creates a new padded bytearray from the msg.data bytearray, leaving
the passed can.Message intact.

Signed-off-by: Bjarki Arge Andreasen <bjarki.andreasen@nordicsemi.no>
@mergify

mergify Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

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