Skip to content

PGN and SPN runtime types truncate DLC, bit length, scale, and enums #8

Closed

Description

@ip332

What happens

Several fields are narrowed or rounded while the DBC is copied into PGN / SPN, so a generated table cannot represent the file even before lookup.

  • DLC. Message::dlc_ is uint16_t. PGN::dlc_ is uint8_t (j1939_frame.h), and Message::toPgn assigns it across. J1939_frame::max_dlc_ is 1758 because a transport-protocol payload can be that long. Any nominal length above 255 is truncated. toStringPgn prints pgn.dlc_ rather than the frame length it actually holds.
  • Signal length. Signal::length_ is uint16_t. SPN::length_ is uint8_t, filled by Signal::toSpn. A signal longer than 255 bits is truncated. Independently, read_bits / write_bits and signalRawValue return a single uint64_t, so a VIN, a NAME-adjacent text field, or any NMEA string longer than 64 bits cannot round-trip. make_mask special-cases 64 and otherwise does 1ul << length. unsigned long is 32 bits on LLP64 and on many 32-bit ECUs, so lengths from 32 to 63 are undefined there. Lengths above 64 are undefined everywhere.
  • Scale and offset. Signal parses the DBC factor with std::stof into a float. SPN::scalar_ and SPN::offset_ are float. min_ / max_ are double. Factors such as 1e-7 or a long decimal do not survive float, and toDouble cannot recover the bits that were already rounded. setSignalValue then divides by that float.
  • Enums. Signal::addValue parses the numeric value with std::stoi (signed 32-bit) and stores it in std::map<uint32_t, std::string>. A value outside int throws. A value above 2^31-1 cannot be stored as the unsigned raw bit pattern it is on the wire. The same function writes every enum label to std::cout, so compiling a DBC that has VAL_ records spams the compiler's stdout.
  • Duplicate start bits. Message::signals_ is std::map<uint16_t, Signal> and addSignal uses emplace. A second signal with the same start bit (the usual shape of a multiplex) is discarded with no diagnostic.

Expected

The C database in the companion proposal uses uint16_t for DLC and bit length, double (or an exact decimal rational) for scale and offset, and an enum value wide enough for the signal (uint64_t). make_mask should be 1ull << length for length < 64, with length 0 and length 64 as explicit cases, and should refuse length > 64 for integer signals.

addValue should not print. A dropped or truncated field should be a compiler error, not a silent narrowing.

Activity

  1. ip332 commented on Nov 19, 2026

    @ip332
    OwnerAuthor

    Fixed in #11.

    PGN::dlc_ and SPN::length_ are uint16_t. Scale and offset stay double from the DBC decimal text. Enum keys are uint64_t, and a VAL_ value that does not fit fails the file. make_mask shifts 1ull. The PGN table keeps every multiplex alternative. Two plain signals at the same start bit fail the compile.

    The pull request is based on issue-4 (#10), so the diff is only this type fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions