fix: correct memcmp misplaced parentheses (size argument in _crcError CRC field detection)#224
Open
m-mcgowan wants to merge 2 commits intoblues:masterfrom
Open
fix: correct memcmp misplaced parentheses (size argument in _crcError CRC field detection)#224m-mcgowan wants to merge 2 commits intoblues:masterfrom
m-mcgowan wants to merge 2 commits intoblues:masterfrom
Conversation
The third argument to memcmp was ((sizeof(CRC_FIELD_NAME_TEST) - 1)) != 0)
which evaluates != 0 as part of the size expression, yielding boolean 1
instead of the intended length 7. This caused CRC field detection to
compare only 1 byte (a quote character) instead of the full pattern.
This is triggered on Notecard firmware that does not support CRC. A
hub.status response like {"connected":false,"status":"connecting"}
has a quote at the CRC detection offset (position 19 = 41 - 22). The
1-byte compare falsely matches it as a CRC field, permanently setting
notecardFirmwareSupportsCrc=true and causing all subsequent responses
to be flagged as CRC errors.
When the Notecard does support CRC, the real CRC field sits at the
expected offset, so the 1-byte compare accidentally matches correctly.
Add regression tests with a realistic hub.status response that triggers
the bug to verify the fix.
824aa92 to
731ff22
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_crcErrorwhere the memcmp return value is not checked correctly —!= 0was intended to test whether memcmp found a match, but instead it's parsed as part of the size argument, reducing a 7-byte comparison to 1 bytehub.statusresponse that triggers the false detectionThe Bug
_crcErrordetects CRC fields by checking 22 bytes before the closing}for the pattern"crc":". The memcmp call was:The
!= 0was intended to test the memcmp return value, but the closing parentheses are misplaced — it's parsed as part of the third argument:(7 != 0)=1. So memcmp only compared 1 byte — the opening"quote — which matches ANY JSON field boundary at that position.When does this trigger?
Only when talking to a Notecard that does not support CRC. note-c always adds CRC to outgoing requests speculatively. When the Notecard supports CRC, it responds with a real
,"crc":"..."field at the expected offset, so the 1-byte compare accidentally matches the correct field and everything works.When the Notecard does not support CRC, responses lack a CRC field. If the response happens to place a
"character at the CRC detection offset (22 bytes before the closing}), the 1-byte compare falsely matches it.For example,
{"connected":false,"status":"connecting"}(41 bytes) — a realhub.statusresponse during connection — has"at position 19 (= 41 - 22), the opening quote of"status". This is a realistic trigger becausehub.statusonly returnsconnected(boolean) andstatus(string) with no other fields. This triggers false CRC detection, which:notecardFirmwareSupportsCrc = trueFix
Test plan
_crcError_testtests pass (CRC detection, validation, edge cases)hub.statusresponse does NOT trigger false CRC detection when firmware does not support CRCnotecardFirmwareSupportsCrcis not falsely set totrue