-
Notifications
You must be signed in to change notification settings - Fork 455
CDRIVER-5667 Add missing BSON type checks in special key handlers #1693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
I am happy to learn "LF" (presumably) means Looking For.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good fix and clean-up! LGTM
} \ | ||
((void) 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be an else
before the cast-to-void?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I omitted the else
because it didn't appear to be necessary. The if
is scoped with {}
, so there is (AFAIK) no opportunity for else
hijacking or other (plausible) mistakes which may produce unexpected behavior at usage sites. A plain ((void)0)
seems sufficient to force a semicolon regardless of the macro definition (if
or otherwise). If I am incorrect and it is advisable to keep the else
anyways, a followup PR can patch it in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is so contained, I don't think there's any risk of anything goofy happening. My only thought was about:
if (x)
HANDLE_OPTION_TYPE_CHECK(...)
else
lol();
But that would just be a hard compile-error anyway.
) * Use X macro for special keys * Use consistent key comparison expressions * CDRIVER-5667 Add missing BSON type checks in special key handlers * Add regression test to /bson/json/errors
Resolves CDRIVER-5667. Verified by this patch.
Traced the issue to a code pattern discrepancy in
_bson_json_read_map_key
when handling special keys in theBSON_JSON_IN_BSON_TYPE
read state. The BSON type field is reset toBSON_TYPE_EOD
(value:0
) when transitioning fromBSON_JSON_IN_START_MAP
->BSON_JSON_IN_BSON_TYPE
. This PR identified that when subsequently identifying the special key value on re-entry into this function (such as when parsing the key of a subdocument), only some branches performed a check for a pre-existing non-EOD BSON type. Simplified:Adding the missing BSON type checks was sufficient to detect and handle the malformed input described in CDRIVER-5667. These checks prevent the state machine from reaching the point where
_bson_json_read_append_code
is invoked with a precondition violation (the top of JSON stack frame is not a previously-parsed scope document) by detecting+forbidding the presence of any intermediate and unrelated BSON types that could overwrite the current JSON stack frame with their own state.Some drive-by improvements include:
strlen
+memcmp
calls for duplicate key checks).strlen
+memcmp
(formerlystrncmp
) pattern into a dedicated macro for consistent reuse.BSON_STR
to reduce need for escape characters.