From d842c6192a5abf9b572893dc6b722d1f33cbef80 Mon Sep 17 00:00:00 2001 From: Wyatt Date: Wed, 17 Apr 2024 20:22:51 -0400 Subject: [PATCH] fix: binary parser sometimes reads out of packet bounds when results contain null and typecast is false --- lib/parsers/binary_parser.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/parsers/binary_parser.js b/lib/parsers/binary_parser.js index 5a08df7b7c..1083204f04 100644 --- a/lib/parsers/binary_parser.js +++ b/lib/parsers/binary_parser.js @@ -169,6 +169,10 @@ 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 { @@ -176,9 +180,6 @@ function compile(fields, options, config) { 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} });`, @@ -186,8 +187,9 @@ function compile(fields, options, config) { } else { parserFn(`${lvalue} = ${readCode};`); } - parserFn('}'); } + parserFn('}'); + currentFieldNullBit *= 2; if (currentFieldNullBit === 0x100) {