Skip to content

canbus: fix frame comparison semantics - #3

Open
Opriego wants to merge 1 commit into
jeremyhahn:masterfrom
Opriego:fix/canbus-frame-comparison
Open

canbus: fix frame comparison semantics#3
Opriego wants to merge 1 commit into
jeremyhahn:masterfrom
Opriego:fix/canbus-frame-comparison

Conversation

@Opriego

@Opriego Opriego commented Aug 17, 2026

Copy link
Copy Markdown

Summary

Fix canbus_framecmp() so CAN frames are compared consistently and as binary data.

The previous implementation had three issues:

  • It returned 1 when CAN IDs differed, but also returned 1 when payloads were equal, resulting in inconsistent comparison semantics.
  • It used strcmp() for CAN payloads, even though CAN data is binary and may contain embedded NUL (0x00) bytes.
  • It did not compare can_dlc, so frames with different payload lengths could be treated incorrectly.

Changes

  • Compare CAN IDs explicitly.

  • Compare DLC values explicitly.

  • Replace strcmp() with memcmp() over exactly can_dlc bytes.

  • Keep comparison semantics consistent:

    • 0 = frames are equal
    • non-zero = frames are different
  • Add regression tests covering:

    • identical frames
    • different CAN IDs
    • different DLC values
    • different payloads
    • binary payload differences after an embedded NUL byte

Testing

The regression suite was first run against the original implementation:

Running suite(s): canbus
60%: Checks: 5, Failures: 2, Errors: 0

The failures demonstrated the inconsistent comparison behavior for identical frames and differing payloads.

After applying the fix:

Running suite(s): canbus
100%: Checks: 5, Failures: 0, Errors: 0

The new check_canbus target was also validated through Automake generation, confirming that both tests/check_canbus.c and src/canbus.c are included in the test executable.

Compare CAN frame IDs and DLC explicitly and use memcmp() for binary payload data. This fixes the inverted payload comparison semantics and avoids treating embedded NUL bytes as string terminators.

Add regression tests covering identical frames, different CAN IDs, different DLC values, different payloads, and binary payload differences after an embedded NUL byte.
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.

1 participant