Skip to content

Commit 705835d

Browse files
authored
fix: binary parser sometimes reads out of packet bounds when results contain null and typecast is false (#2601)
1 parent 2129818 commit 705835d

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

lib/parsers/binary_parser.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,25 +169,27 @@ function compile(fields, options, config) {
169169
lvalue = `result[${fieldName}]`;
170170
}
171171

172+
parserFn(`if (nullBitmaskByte${nullByteIndex} & ${currentFieldNullBit}) `);
173+
parserFn(`${lvalue} = null;`);
174+
parserFn('else {');
175+
172176
if (options.typeCast === false) {
173177
parserFn(`${lvalue} = packet.readLengthCodedBuffer();`);
174178
} else {
175179
const fieldWrapperVar = `fieldWrapper${i}`;
176180
parserFn(`const ${fieldWrapperVar} = wrap(fields[${i}], packet);`);
177181
const readCode = readCodeFor(fields[i], config, options, i);
178182

179-
parserFn(`if (nullBitmaskByte${nullByteIndex} & ${currentFieldNullBit})`);
180-
parserFn(`${lvalue} = null;`);
181-
parserFn('else {');
182183
if (typeof options.typeCast === 'function') {
183184
parserFn(
184185
`${lvalue} = options.typeCast(${fieldWrapperVar}, function() { return ${readCode} });`,
185186
);
186187
} else {
187188
parserFn(`${lvalue} = ${readCode};`);
188189
}
189-
parserFn('}');
190190
}
191+
parserFn('}');
192+
191193

192194
currentFieldNullBit *= 2;
193195
if (currentFieldNullBit === 0x100) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const common = require('../../common.test.cjs');
4+
const connection = common.createConnection({
5+
typeCast: false,
6+
});
7+
8+
const { assert } = require('poku');
9+
10+
const COL_1_VALUE = 'col v1';
11+
const COL_2_VALUE = 'col v2';
12+
13+
function executeTests(res) {
14+
assert.equal(res[0].v1.toString('ascii'), COL_1_VALUE);
15+
assert.equal(res[0].n1, null);
16+
assert.equal(res[0].v2.toString('ascii'), COL_2_VALUE);
17+
}
18+
19+
connection.query(
20+
'CREATE TEMPORARY TABLE binpar_null_test (v1 VARCHAR(16) NOT NULL, n1 VARCHAR(16), v2 VARCHAR(16) NOT NULL)',
21+
);
22+
connection.query(
23+
`INSERT INTO binpar_null_test (v1, n1, v2) VALUES ("${COL_1_VALUE}", NULL, "${COL_2_VALUE}")`,
24+
(err) => {
25+
if (err) throw err;
26+
},
27+
);
28+
29+
connection.execute('SELECT * FROM binpar_null_test', (err, res) => {
30+
if (err) throw err;
31+
executeTests(res);
32+
connection.end();
33+
});

0 commit comments

Comments
 (0)