Skip to content

When parsing DBC, make sure there are no unused VAL_ and SIG_VALTYPE_ entries - #661

Merged
andlaus merged 4 commits into
cantools:masterfrom
SebKuzminsky:validate-dbc
Oct 14, 2026
Merged

andlaus merged 4 commits into
cantools:masterfrom
SebKuzminsky:validate-dbc

Conversation

@SebKuzminsky

Copy link
Copy Markdown
Contributor

This makes DBC parsing a little more strict, rejecting DBCs with some common(?) errors.

@coveralls

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 8840594508

Details

  • 14 of 20 (70.0%) changed or added relevant lines in 1 file are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.06%) to 93.489%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/cantools/database/can/formats/dbc.py 14 20 70.0%
Files with Coverage Reduction New Missed Lines %
src/cantools/database/can/formats/dbc.py 1 96.7%
Totals Coverage Status
Change from base Build 8024886298: -0.06%
Covered Lines: 7266
Relevant Lines: 7772

💛 - Coveralls

@SebKuzminsky

Copy link
Copy Markdown
Contributor Author

@andlaus do you want me to rebase this on top of master to pick up the linter fix that you just merged?

@zariiii9003

Copy link
Copy Markdown
Collaborator

I'm not sure about this. I do not see the benefit of raising an exception on unused values. If CANdb++ can open these files, then they are valid and we should accept them.

@andlaus

andlaus commented May 3, 2024

Copy link
Copy Markdown
Member

@andlaus do you want me to rebase this on top of master to pick up the linter fix that you just merged?

that would probably a good idea...

@SebKuzminsky

SebKuzminsky commented May 3, 2024 •

Copy link
Copy Markdown
Contributor Author

I'm not sure about this. I do not see the benefit of raising an exception on unused values. If CANdb++ can open these files, then they are valid and we should accept them.

CANDB++ fails to open these files (and prints a very unhelpful error message). Current cantools (39.4.5) accepts these bogus/questionable files. This PR brings cantools into closer alignment with the CANDB++ parser.

@SebKuzminsky SebKuzminsky reopened this May 3, 2024
@SebKuzminsky

Copy link
Copy Markdown
Contributor Author

(Oops, didn't mean to close this PR!)

@SebKuzminsky

Copy link
Copy Markdown
Contributor Author

And this latest force-push should fix the ruff linter error i introduced. 🙄

Sorry for all the churn...

@SebKuzminsky

Copy link
Copy Markdown
Contributor Author

Here are some small DBC files that demonstrate the issue. They both define two packets with one signal each. In bad.dbc the VAL_ and SIG_VALTYPE_ lines have incorrect frame ids (COB IDs?) so they don't get applied to the signals. In good.dbc the frame ids have been fixed so the lines do get applied to the signals.

good.dbc.txt
bad.dbc.txt

The diff from bad to good is this:

$ diff -u bad.dbc good.dbc 
--- bad.dbc     2024-05-03 15:10:47.601981628 -0600
+++ good.dbc    2024-05-03 15:10:53.985999971 -0600
@@ -42,6 +42,6 @@
  SG_ signal2 : 0|32@1- (1,0) [0|0] "" Vector__XXX
 
 
-VAL_ 99 signal1 3 "High" 2 "Standard" 1 "Low" 0 "Unknown" ;
+VAL_ 1 signal1 3 "High" 2 "Standard" 1 "Low" 0 "Unknown" ;
 
-SIG_VALTYPE_ 345 signal2 : 1;
+SIG_VALTYPE_ 2 signal2 : 1;

Here is CANdb++ trying to load bad.dbc:
Screenshot from 2024-05-03 15-11-24
Screenshot from 2024-05-03 15-11-29

Here is cantools accepting bad.dbc and just ignoring the invalid VAL_ and SIG_VALTYPE_ lines, so the signals are interpreted as "default" integers:

$ python3 -m cantools list --all bad.dbc 
msg1:
  Frame ID: 0x1 (1)
  Size: 1 bytes
  Is extended frame: False
  Is CAN-FD frame: False
  Signal tree:

    -- {root}
       +-- signal1

  Signal details:
    signal1:
      Internal type: Integer
      Start bit: 0
      Length: 8 bits
      Byte order: little_endian
      Is signed: False
msg2:
  Frame ID: 0x2 (2)
  Size: 4 bytes
  Is extended frame: False
  Is CAN-FD frame: False
  Signal tree:

    -- {root}
       +-- signal2

  Signal details:
    signal2:
      Internal type: Integer
      Start bit: 0
      Length: 32 bits
      Byte order: little_endian
      Is signed: True

Here is CANdb++ successfully loading good.dbc:
Screenshot from 2024-05-03 15-11-57

And for completeness, here is cantools parsing good.dbc:

$ python3 -m cantools list --all good.dbc 
msg1:
  Frame ID: 0x1 (1)
  Size: 1 bytes
  Is extended frame: False
  Is CAN-FD frame: False
  Signal tree:

    -- {root}
       +-- signal1

  Signal details:
    signal1:
      Internal type: Integer
      Start bit: 0
      Length: 8 bits
      Byte order: little_endian
      Is signed: False
      Named values:
        3: High
        2: Standard
        1: Low
        0: Unknown
msg2:
  Frame ID: 0x2 (2)
  Size: 4 bytes
  Is extended frame: False
  Is CAN-FD frame: False
  Signal tree:

    -- {root}
       +-- signal2

  Signal details:
    signal2:
      Internal type: Float
      Start bit: 0
      Length: 32 bits
      Byte order: little_endian
      Is signed: True

@andlaus

andlaus commented Oct 2, 2026

Copy link
Copy Markdown
Member

hm, I wonder about the use case of this. Is it if the DBC was written by an external tool (not using cantools) and the result is only read by a script based on cantools? if yes, I think there's no harm in cantools accepting slightly incorrect files. (The developers of the external tool should occasionally check if its results can be loaded by candb++...)

@SebKuzminsky

Copy link
Copy Markdown
Contributor Author

hm, I wonder about the use case of this.

The use case is validating a DBC.

I am writing software to communicate over CAN with a device made by a different team (or rather, i was 2.5 years ago when i opened this PR). The team that made the device publishes a DBC to describe their packet structure. I don't know how they wrote their DBC, or what tools/editors they used, all i have is their DBC. Their DBC contained a number of errors, and i wanted a tool to help me find these errors so i could report them back to the producer of the DBC for them to fix.

Since i was already using cantools in my project, and since cantools had already found a number of other errors in the DBC, this change seemed in scope to me. If this is not a use case you want cantools to support then please feel free to close this PR and discard.

@andlaus

andlaus commented Oct 2, 2026

Copy link
Copy Markdown
Member

okay. If you are confident that with this feature cantools can still load any DBC which is accepted by candb++, I'll merge the PR after it has been rebased on the latest master and a unit test for the new code paths has been added.

@SebKuzminsky

Copy link
Copy Markdown
Contributor Author

I make no general claims about the relationship between this branch and the candb++ parser, beyond the spot-check shown above. In that case, with the two DBC files i tested, this PR makes cantools behave more like the candb++ parser.

@andlaus

andlaus commented Oct 3, 2026

Copy link
Copy Markdown
Member

I think we can risk it. If we get bug reports about DBCs not loading anymore, it should be reverted rather quickly...

(rebasing it on master and a unit test is required for merging, though.)

@andlaus

andlaus commented Oct 13, 2026

Copy link
Copy Markdown
Member

@SebKuzminsky: Any appetite for rebasing this on the current master? (if not, please close the PR.)

@SebKuzminsky

Copy link
Copy Markdown
Contributor Author

Rebased on top of master, added tests. Happy to make any changes requested.

@andlaus

andlaus commented Oct 13, 2026

Copy link
Copy Markdown
Member

Rebased on top of master, added tests. Happy to make any changes requested.

hm, github still says that there are merge conflicts. did you pull your master to the latest version before rebasing?

@andlaus

andlaus commented Oct 13, 2026

Copy link
Copy Markdown
Member

(there was a pretty big cleanup of the DBC code recently, though it did not change fundamentally.)

@SebKuzminsky

Copy link
Copy Markdown
Contributor Author

Oops, you're right, i used an older master. I'll try again to fix it.

Sebastian Kuzminsky and others added 4 commits October 13, 2026 18:14
I came across a DBC in the wild which contained a VAL_ line that specified
the wrong frame ID for its signal.  cantools accepted the DBC file,
but failed to interpret the signal correctly.

This commit tries to detect this DBC error by removing VAL_ choices
as they are used by Messages, and raising an exception if any choices
remain after parsing the entire DBC.
I came across a DBC in the wild which contained a SIG_VALTYPE_ line that
specified the wrong frame ID for its signal.  cantools accepted the DBC
file, but failed to interpret the signal correctly.

This commit tries to detect this DBC error by removing SIG_VALTYPE_
choices from the signal_types dict as they are used by Messages, and
raising an exception if any entries remain after parsing the entire DBC.
@SebKuzminsky

Copy link
Copy Markdown
Contributor Author

This version should be good, let me know if you want any further changes.

@andlaus

andlaus commented Oct 14, 2026

Copy link
Copy Markdown
Member

I'm happy now. Let's jump the gun and take it back if we get complaints. Thanks for your patience.

@andlaus
andlaus merged commit 8d88413 into cantools:master Oct 14, 2026
20 checks passed
andlaus added a commit that referenced this pull request Oct 29, 2026
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.

4 participants