Check field_code and type_code when readFieldOrdinal() is called (#81)

Added verification that nth and type are valid when read from BinaryParser
This commit is contained in:
Nathan Nichols
2020-07-07 13:58:24 -05:00
parent 485ec4e924
commit 8ac03699aa
2 changed files with 35 additions and 8 deletions

View File

@@ -107,9 +107,24 @@ class BinaryParser {
* @return Field ordinal
*/
readFieldOrdinal(): number {
const tagByte = this.readUInt8();
const type = (tagByte & 0xf0) >>> 4 || this.readUInt8();
const nth = tagByte & 0x0f || this.readUInt8();
let type = this.readUInt8();
let nth = type & 15;
type >>= 4;
if (type === 0) {
type = this.readUInt8();
if (type === 0 || type < 16) {
throw new Error("Cannot read FieldOrdinal, type_code out of range");
}
}
if (nth === 0) {
nth = this.readUInt8();
if (nth === 0 || nth < 16) {
throw new Error("Cannot read FieldOrdinal, field_code out of range");
}
}
return (type << 16) | nth;
}