Skip to content

Commit

Permalink
fix: binary parser sometimes reads out of packet bounds when results …
Browse files Browse the repository at this point in the history
…contain null and typecast is false
  • Loading branch information
Parsonswy committed Apr 18, 2024
1 parent 2129818 commit d842c61
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/parsers/binary_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,25 +169,27 @@ function compile(fields, options, config) {
lvalue = `result[${fieldName}]`;
}

parserFn(`if (nullBitmaskByte${nullByteIndex} & ${currentFieldNullBit}) `);
parserFn(`${lvalue} = null;`);
parserFn('else {');

if (options.typeCast === false) {
parserFn(`${lvalue} = packet.readLengthCodedBuffer();`);
} else {
const fieldWrapperVar = `fieldWrapper${i}`;
parserFn(`const ${fieldWrapperVar} = wrap(fields[${i}], packet);`);
const readCode = readCodeFor(fields[i], config, options, i);

parserFn(`if (nullBitmaskByte${nullByteIndex} & ${currentFieldNullBit})`);
parserFn(`${lvalue} = null;`);
parserFn('else {');
if (typeof options.typeCast === 'function') {
parserFn(
`${lvalue} = options.typeCast(${fieldWrapperVar}, function() { return ${readCode} });`,
);
} else {
parserFn(`${lvalue} = ${readCode};`);
}
parserFn('}');
}
parserFn('}');


currentFieldNullBit *= 2;
if (currentFieldNullBit === 0x100) {
Expand Down

0 comments on commit d842c61

Please sign in to comment.