-
Notifications
You must be signed in to change notification settings - Fork 75
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
V2: Check for matching field numbers in StartGroup / EndGroup tags #810
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,12 +108,12 @@ function readMessage( | |
const unknownFields = message.getUnknown() ?? []; | ||
while (reader.pos < end) { | ||
[fieldNo, wireType] = reader.tag(); | ||
if (wireType == WireType.EndGroup) { | ||
if (delimited && wireType == WireType.EndGroup) { | ||
break; | ||
} | ||
const field = message.findNumber(fieldNo); | ||
if (!field) { | ||
const data = reader.skip(wireType); | ||
const data = reader.skip(wireType, fieldNo); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
if (options.readUnknownFields) { | ||
unknownFields.push({ no: fieldNo, wireType, data }); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -374,10 +374,12 @@ export class BinaryReader { | |
} | ||
|
||
/** | ||
* Skip one element on the wire and return the skipped data. | ||
* Supports WireType.StartGroup since v2.0.0-alpha.23. | ||
* Skip one element and return the skipped data. | ||
* | ||
* When skipping StartGroup, provide the tags field number to check for | ||
* matching field number in the EndGroup tag. | ||
*/ | ||
skip(wireType: WireType): Uint8Array { | ||
skip(wireType: WireType, fieldNo?: number): Uint8Array { | ||
let start = this.pos; | ||
switch (wireType) { | ||
case WireType.Varint: | ||
|
@@ -398,10 +400,15 @@ export class BinaryReader { | |
this.pos += len; | ||
break; | ||
case WireType.StartGroup: | ||
// TODO check for matching field numbers in StartGroup / EndGroup tags | ||
let t: WireType; | ||
while ((t = this.tag()[1]) !== WireType.EndGroup) { | ||
this.skip(t); | ||
for (;;) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting syntax. Is this idiomatic for JS/TS? In languages with There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 |
||
const [fn, wt] = this.tag(); | ||
if (wt === WireType.EndGroup) { | ||
if (fieldNo !== undefined && fn !== fieldNo) { | ||
throw new Error("invalid end group tag"); | ||
} | ||
break; | ||
} | ||
this.skip(wt, fn); | ||
} | ||
break; | ||
default: | ||
|
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.
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.