-
Notifications
You must be signed in to change notification settings - Fork 4
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
Use lengthCode to determine whether to read sequenceLength again #101
Changes from 2 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 |
---|---|---|
|
@@ -191,10 +191,11 @@ export class MessageReader<T = unknown> { | |
): unknown { | ||
const { readMemberHeader, parentName } = emHeaderOptions; | ||
const definitionId = getDefinitionId(field); | ||
let emHeaderSizeBytes = undefined; | ||
let emHeaderSizeBytes; | ||
if (readMemberHeader) { | ||
const { id, objectSize: objectSizeBytes } = reader.emHeader(); | ||
emHeaderSizeBytes = objectSizeBytes; | ||
const { id, objectSize: objectSizeBytes, lengthCode } = reader.emHeader(); | ||
emHeaderSizeBytes = useEmHeaderAsLength(lengthCode) ? objectSizeBytes : undefined; | ||
|
||
// this can help spot misalignments in reading the data | ||
if (definitionId != undefined && id !== definitionId) { | ||
throw Error( | ||
|
@@ -236,8 +237,6 @@ export class MessageReader<T = unknown> { | |
} | ||
const headerSpecifiedLength = | ||
emHeaderSizeBytes != undefined ? Math.floor(emHeaderSizeBytes / typeLength) : undefined; | ||
const remainderBytes = | ||
emHeaderSizeBytes != undefined ? emHeaderSizeBytes % typeLength : undefined; | ||
|
||
if (field.isArray === true) { | ||
const deser = typedArrayDeserializers.get(field.type); | ||
|
@@ -261,15 +260,7 @@ export class MessageReader<T = unknown> { | |
// last arrayLengths length is handled in deserializer. It returns an array | ||
return readNestedArray(typedArrayDeserializer, arrayLengths.slice(0, -1), 0); | ||
} else { | ||
// Special case can happen where the emHeader is not divisible by the typeLength and there is a remainder of 4 bytes. | ||
// 4 bytes of 0's are printed after the header length | ||
// This can happen for 8 byte typeLength types. | ||
Comment on lines
-264
to
-266
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. was this just wrong? or where is the sequence length getting read now? 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. Yeah this was just wrong because I didn't know about the lengthCode caveat. Previously I was always using emHeader size instead of sequenceLength. This case would've arisen for when the lengthCode was <5 and the object size was less than the size of the type. So there was an emHeader object size of 4 and a sequence length. Now that I'm accounting for length code, I will read the sequenceLength earlier in the array handling because the emHeaderSize isn't being used. |
||
if (arrayLengths[0] === 0 && remainderBytes === 4) { | ||
reader.sequenceLength(); | ||
return deser(reader, 0); | ||
} else { | ||
return deser(reader, arrayLengths[0]!); | ||
} | ||
return deser(reader, arrayLengths[0]!); | ||
} | ||
} else { | ||
const deser = deserializers.get(field.type); | ||
|
@@ -422,3 +413,8 @@ function getCaseForDiscriminator( | |
} | ||
return unionDef.defaultCase; | ||
} | ||
|
||
/** Only length Codes >= 5 should allow for emHeader sizes to be used in place of other integer lengths */ | ||
function useEmHeaderAsLength(lengthCode: number | undefined): boolean { | ||
return lengthCode != undefined && lengthCode >= 5; | ||
} |
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.
is the reader supposed to be able to handle this both ways (length code ≥5 or <5)? If so, should we have another test which uses a length code <5?
(same applies to below test as well)
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.
Yeah that's correct. I'll add some more tests to account for this.