Skip to content

V2: Check for matching field numbers in StartGroup / EndGroup tags#810

Merged
timostamm merged 2 commits intov2from
tstamm/check-group-field-no-when-skipping
Apr 25, 2024
Merged

V2: Check for matching field numbers in StartGroup / EndGroup tags#810
timostamm merged 2 commits intov2from
tstamm/check-group-field-no-when-skipping

Conversation

@timostamm
Copy link
Member

When an unknown field is encountered while parsing binary, the tag is skipped. This adds a check for skipping groups, verifying that the field number of StartGroup and EndGroup matches.

Copy link
Member Author

@timostamm timostamm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be ported to main / v1.

while (reader.pos < end) {
[fieldNo, wireType] = reader.tag();
if (wireType == WireType.EndGroup) {
if (delimited && wireType == WireType.EndGroup) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only break the loop for EndGroup if we're currently parsing a group (a.k.a. delimited message encoding).

Previously, this would behave incorrectly for a rogue EndGroup tag.

const field = message.findNumber(fieldNo);
if (!field) {
const data = reader.skip(wireType);
const data = reader.skip(wireType, fieldNo);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to pass the field number, so that the EndGroup tag's field number can be checked.

@timostamm timostamm marked this pull request as ready for review April 24, 2024 17:37
@timostamm timostamm requested a review from jhump April 24, 2024 17:39
Copy link
Member

@jhump jhump left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

let t: WireType;
while ((t = this.tag()[1]) !== WireType.EndGroup) {
this.skip(t);
for (;;) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting syntax. Is this idiomatic for JS/TS? In languages with while keywords, I am used to instead seeing while (true).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be idiomatic to avoid loops, which is not feasible here. I would prefer while (true) as well, but the linter objects the constant condition.

It's possible that the linter can be configured to accept it, but touching the linter config is always very involved. Since we have to change the linter config to replace make with turborepo, it's better to punt on it for now. Our usage of make in this repo is much more unidiomatic than this loop construct.

@timostamm timostamm merged commit 9d2cbe1 into v2 Apr 25, 2024
@timostamm timostamm deleted the tstamm/check-group-field-no-when-skipping branch April 25, 2024 07:43
smaye81 pushed a commit that referenced this pull request Apr 29, 2024
This is a port of #810 from
the v2 branch into the main (v1) branch.

---------

Co-authored-by: Timo Stamm <[email protected]>
@timostamm timostamm mentioned this pull request May 13, 2024
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.

2 participants