Skip to content

fix: binary parser sometimes reads out of packet bounds when results … #2601

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

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});