Repository navigation
Added ACF_CRC format along with unit tests. - #228
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The CRC validator accepts header-only messages for fixed four-byte CRC types, allowing receivers that trust validation to read past the buffer.
1 open issue
What changed in this PR
Adds ACF_CRC message support to Open1722, including serialization helpers, validation, and unit tests.
Changes:
- Introduces the CRC header layout, CRC types, and message-handling APIs.
- Exports the new APIs and documents CRC as a supported format.
- Adds tests for initialization, fields, payload padding, message creation, and validation.
| File | Description |
|---|---|
unit/test-crc.c |
Adds unit tests for ACF_CRC message handling. |
unit/CMakeLists.txt |
Registers the CRC tests for dual-mode execution. |
src/avtp/export/InlineExports.c |
Includes CRC APIs in inline exports. |
README.md |
Lists CRC among supported ACF formats. |
include/avtp/acf/Crc.h |
Defines the CRC format and message-handling APIs. |
Review effort: Balanced
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #228 +/- ##
========================================
Coverage 99.56% 99.57%
========================================
Files 61 63 +2
Lines 9489 9699 +210
Branches 334 354 +20
========================================
+ Hits 9448 9658 +210
Misses 40 40
Partials 1 1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
The "large" comments I added to the relevant code segements. Here are some minor AI nitpicks you may or may nor want to tackle:
- While updating the tests, add coverage for:
- header-only CRC_USER is rejected;
- GetPayloadLength() (if added) returns 4 for CRC_ETH/CRC_32P4 and the variable length for CRC_USER.
- Crc.h:257-258 calls Avtp_Crc_GetCrcType() twice. A local variable or a switch would be clearer and fits naturally with the minimum-length check above.
- The convenience CreateAcfMessage() (Crc.h:207-221) does not reject payload_length != 4 for CRC_ETH/CRC_32P4. That is consistent with other formats’ Create functions, which assume caller correctness, but documenting the precondition would help.
- GetPayload/SetPayload use the generic payload naming while the spec field is crc_data. That is acceptable under the ACF payload convention, but callers must remember that crc_data for types 0/1 is a network-byte-order 32-bit value; SetPayload() copies bytes verbatim. A typed Get/SetCrcData(uint32_t) accessor for the fixed-width types, or at least an endianness note, would be more consistent with Avtp_Checksum_Get/SetChecksum in PR #229. This is a design suggestion, not a spec violation.
I think we discussed this, but for completeless sake
This PR implements the ACF_CRC container only. It does not compute Ethernet CRC-32 or AUTOSAR CRC-32P4, nor does it validate a received CRC over the preceding ACF message. If “ACF_CRC support” is intended to mean the full listener behaviour in §9.4.21.1, this is incomplete. If it is intended to be format support only — consistent with the Checksum PR #229, which also only carries the value — then that should probably be stated in the PR description so it is clear that CRC calculation is a follow-up.
- Added function for accessing CRC data - Disallowed ACF_CRC without CRC data - Fixed length for CRC_ETH and CRC32P4 - Improved test coverage Signed-off-by: Naresh Nayak <Naresh.Nayak@hs-furtwangen.de>
Tests added for these scenarios. Though for the GetPayloadLength(), the real values are returned irrespective of the CRC type.
Used a local variable
Added an overlapping field CRC_DATA with the payload. The convenience function chooses the right field to set based on the CRC type.
This should now be fixed with an overlapping field with the payload data.
Is also included in the PR description now |
SebastianSchildt
left a comment
There was a problem hiding this comment.
See comments in code.
There is also a style thing:
AI says
API/convention issue: CRC_DATA was added to the header descriptor table
Crc.h:86-88 and Crc.h:113 add AVTP_CRC_FIELD_CRC_DATA at quadlet 1, but AVTP_CRC_HEADER_LEN is only 4 bytes. The project guidance says the format field enum/table describes the header, covering every header bit exactly once (docs/api-design.md:433-491). The new test name crc_field_descriptors_cover_header now covers 8 bytes and no longer asserts quadlet == 0, so the convention is no longer enforced.
The typed accessor itself is useful and works, but it should not make Avtp_CrcFieldDesc a header-plus-payload table. Options:
- Keep the header table header-only and implement Get/SetCrcData over pdu->payload using the existing Avtp_BeToCpu32()/Avtp_CpuToBe32() helpers, or
- Use a separate descriptor for the fixed 32-bit data view, or
- If this exception is intentional, update docs/api-design.md, the enum comment, and the test name to say the table covers “header + first CRC data quadlet”.
Sebastian says
I think we maybe can just remove the entry from the header descriptor table and let Get/setCrcData deal with with it internally?
| // The function will only set the first 4 bytes of the payload as the CRC data. | ||
| // The endianness of the payload will be adjusted based on the host endianness. | ||
| uint32_t crc_data; | ||
| memcpy(&crc_data, payload, sizeof(crc_data)); |
There was a problem hiding this comment.
OOB read:
AI said
always reads 4 bytes for fixed types, regardless of payload_length. Even if the documented contract says “payload length must be 4,” the function should not read beyond the caller-supplied length.
This is a real OOB:
uint8_t payload[1] = {0};
Avtp_Crc_CreateAcfMessage(crc, AVTP_CRC_TYPE_CRC_ETH, payload, 0);ASan reports:
READ of size 4 ... in Avtp_Crc_CreateAcfMessage include/avtp/acf/Crc.h:277
It also silently truncates a longer payload: passing 8 bytes for a fixed type keeps only the first 4 and produces a valid 8-byte message instead of an invalid 12-byte one.
Recommended fix: copy exactly payload_length bytes and let IsValid() reject non-4-byte fixed-type messages, consistent with other CreateAcfMessage() implementations. If the function should enforce 4 bytes, it must at least check payload_length before reading.
Sebastian says:
I would probably check if it is 4 bytes and just do not nothing if it isn't for the fixed length types. But definitely make sure not to overread
| { | ||
| (void)state; | ||
| uint8_t pdu[MAX_PDU_SIZE]; | ||
| uint8_t payload[8] = {0}; |
There was a problem hiding this comment.
The test itself has an OOB read
unit/test-crc.c:182 declares:
uint8_t payload[8] = {0};but unit/test-crc.c:199 calls:
Avtp_Crc_CreateAcfMessage(crc, type, payload, 12);For CRC_USER, this copies 12 bytes from an 8-byte array. ASan reports a stack-buffer-overflow at Crc.h:177/:284, called from test-crc.c:199. The normal and UBSan builds pass because CI does not run ASan, but the test is wrong. Increase the array to at least 12 bytes, or drop the 12-byte “too long” case.
Sebastian says:
note to self ASan might be another wonderful tool to make our CI more annoying!
|
Before we read too much into what AI has to say, we should consider some of the peculiarities of the ACF_CRC format. Status Quo:Open1722 API is designed to have accessor (get/set) functions for different header fields of the corresponding data formats. In most of the data formats, there is a clear distinction between header and payload, which the specification also notes. However, in ACF_CRC (along with the I2C and the Checksum formats) this distinction is not very clear. Header vs. PayloadMy current mental model about what counts as header and what counts as payload:
Does this differentiation help in ACF_CRC data format?Unfortunately, no. Even if you go by the above definition, ACF_CRC is still brings its own peculiarities. ACF_CRC has CRC Data at the end of the data format. From the three supported CRC types in the IEEE 1722 spec., two types have a 32-bit CRC, while there is a user-defined CRC type with an arbitrarily long quadlet-aligned CRC data. Proposed solutionTreat ACF_CRC as an exception.
Quirks
Quite a few open points regarding design requiring human opinions before an AI can jump in. |
|
My view is: crc_data is payload for this ACF and thus I would define header lenght as the length without the crc. And I would not defined crc_data in the header field descriptor table to make it consistent. So my view is: crc_data is not part of the header and should probably not be in the header descriptor table. I.e. CAN or LIN also do not define payload in header, (even though for Lin with max 2 quadlets payload that would be possible). That would be a correct form. Then the question is, what convenience we give for the defined CRC. I think one could use additional convenience setters/getters for getCrcEth or setCrc32P4 in addition the raw setPayload that internally deal with setting the correct payload (and also deal with endianess, whteher they do it "by hand" or by defining a field descriptor and going through the field accessor engine I would consider an implementation detail). Maybe the Create-Functions would need to be split as well, but not sure, or maybe they only support the 4 byte types and if you want an "user" you need to construct it yourself with init and the appropriate setters/getters |
|
Proposal from @SebastianSchildt looks good to me. Since ACF_CRC (in contrary to ACF_Checksum) gives the flexibility to support variable size CRCs it makes sense to treat the CRC data as payload. Providing different create functions to support the most common CRC methods, would definitely help usability. To my knowledge there are only 2-3 CRC options anyway. I'm not sure if it's really that helpful to provide explicit getters for all kinds of CRC types instead of a generic getter for the payload aka CRC data. It might be more helpful to provide a validate function to check validity of a byte array (typically another ACF message) based on the CRC data and type. Something like
But in principle this can also be done in another PR. I can also look into adding support for the AUTOSAR CRC-32 in case you need help. |
- CRC Data is now handled using an accessor - AcfCreateMessage creates only PDUs for fixed length CRC - CRC Data does not overlap with header fields. Signed-off-by: Naresh Nayak <Naresh.Nayak@hs-furtwangen.de>
Signed-off-by: Naresh Nayak <Naresh.Nayak@hs-furtwangen.de>
Signed-off-by: Naresh Nayak <Naresh.Nayak@hs-furtwangen.de>
- Added unit tests for ACF_CRC to improve coverage. - Refactored conditions in IsValid() for better test coverage. Signed-off-by: Naresh Nayak <Naresh.Nayak@hs-furtwangen.de>
- Added function for accessing CRC data - Disallowed ACF_CRC without CRC data - Fixed length for CRC_ETH and CRC32P4 - Improved test coverage Signed-off-by: Naresh Nayak <Naresh.Nayak@hs-furtwangen.de>
- CRC Data is now handled using an accessor - AcfCreateMessage creates only PDUs for fixed length CRC - CRC Data does not overlap with header fields. Signed-off-by: Naresh Nayak <Naresh.Nayak@hs-furtwangen.de>
Signed-off-by: Naresh Nayak <Naresh.Nayak@hs-furtwangen.de>
c423f5a to
1934359
Compare
SebastianSchildt
left a comment
There was a problem hiding this comment.
This is looking good now I think. Let's merge ⚙️
- Added function for accessing CRC data - Disallowed ACF_CRC without CRC data - Fixed length for CRC_ETH and CRC32P4 - Improved test coverage Signed-off-by: Naresh Nayak <Naresh.Nayak@hs-furtwangen.de>
- CRC Data is now handled using an accessor - AcfCreateMessage creates only PDUs for fixed length CRC - CRC Data does not overlap with header fields. Signed-off-by: Naresh Nayak <Naresh.Nayak@hs-furtwangen.de>

Implements only the accessors. #230 will provide the CRC and checksum function implementations.