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 (#2601)
  • Loading branch information
Parsonswy authored Apr 18, 2024
1 parent 2129818 commit 705835d
Show file tree
Hide file tree
Showing 2 changed files with 39 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
33 changes: 33 additions & 0 deletions test/integration/config/test-typecast-global-false.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const common = require('../../common.test.cjs');
const connection = common.createConnection({
typeCast: false,
});

const { assert } = require('poku');

const COL_1_VALUE = 'col v1';
const COL_2_VALUE = 'col v2';

function executeTests(res) {
assert.equal(res[0].v1.toString('ascii'), COL_1_VALUE);
assert.equal(res[0].n1, null);
assert.equal(res[0].v2.toString('ascii'), COL_2_VALUE);
}

connection.query(
'CREATE TEMPORARY TABLE binpar_null_test (v1 VARCHAR(16) NOT NULL, n1 VARCHAR(16), v2 VARCHAR(16) NOT NULL)',
);
connection.query(
`INSERT INTO binpar_null_test (v1, n1, v2) VALUES ("${COL_1_VALUE}", NULL, "${COL_2_VALUE}")`,
(err) => {
if (err) throw err;
},
);

connection.execute('SELECT * FROM binpar_null_test', (err, res) => {
if (err) throw err;
executeTests(res);
connection.end();
});

0 comments on commit 705835d

Please sign in to comment.