Skip to content

Commit 225c5c3

Browse files
committed
Avoid leaking TextRow/BinaryRow object outside the framework
Avoid leaking TextRow/BinaryRow object outside the framework by returning a new object every time instead of returning this
1 parent 0dd8e16 commit 225c5c3

File tree

5 files changed

+19
-22
lines changed

5 files changed

+19
-22
lines changed

lib/commands/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class Query extends Command {
238238
}
239239
let row;
240240
try {
241-
row = new this._rowParser(
241+
row = this._rowParser(
242242
packet,
243243
this._fields[this._resultIndex],
244244
this.options,

lib/parsers/binary_parser.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ function compile(fields, options, config) {
9393

9494
if (options.rowsAsArray) {
9595
parserFn(`const result = new Array(${fields.length});`);
96+
} else {
97+
parserFn("const result = {};");
9698
}
9799

98100
const resultTables = {};
@@ -104,7 +106,7 @@ function compile(fields, options, config) {
104106
}
105107
resultTablesArray = Object.keys(resultTables);
106108
for (i = 0; i < resultTablesArray.length; i++) {
107-
parserFn(`this[${helpers.srcEscape(resultTablesArray[i])}] = {};`);
109+
parserFn(`result[${helpers.srcEscape(resultTablesArray[i])}] = {};`);
108110
}
109111
}
110112

@@ -125,16 +127,16 @@ function compile(fields, options, config) {
125127

126128
if (typeof options.nestTables === 'string') {
127129
tableName = helpers.srcEscape(fields[i].table);
128-
lvalue = `this[${helpers.srcEscape(
130+
lvalue = `result[${helpers.srcEscape(
129131
fields[i].table + options.nestTables + fields[i].name
130132
)}]`;
131133
} else if (options.nestTables === true) {
132134
tableName = helpers.srcEscape(fields[i].table);
133-
lvalue = `this[${tableName}][${fieldName}]`;
135+
lvalue = `result[${tableName}][${fieldName}]`;
134136
} else if (options.rowsAsArray) {
135137
lvalue = `result[${i.toString(10)}]`;
136138
} else {
137-
lvalue = `this[${helpers.srcEscape(fields[i].name)}]`;
139+
lvalue = `result[${helpers.srcEscape(fields[i].name)}]`;
138140
}
139141

140142
// TODO: this used to be an optimisation ( if column marked as NOT_NULL don't include code to check null
@@ -158,9 +160,7 @@ function compile(fields, options, config) {
158160
}
159161
}
160162

161-
if (options.rowsAsArray) {
162-
parserFn('return result;');
163-
}
163+
parserFn('return result;');
164164

165165
parserFn('};')('})()');
166166

lib/parsers/text_parser.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ function compile(fields, options, config) {
111111

112112
if (options.rowsAsArray) {
113113
parserFn(`const result = new Array(${fields.length})`);
114+
} else {
115+
parserFn("const result = {}");
114116
}
115117

116118
if (typeof options.typeCast === 'function') {
@@ -126,7 +128,7 @@ function compile(fields, options, config) {
126128
}
127129
resultTablesArray = Object.keys(resultTables);
128130
for (i = 0; i < resultTablesArray.length; i++) {
129-
parserFn(`this[${helpers.srcEscape(resultTablesArray[i])}] = {};`);
131+
parserFn(`result[${helpers.srcEscape(resultTablesArray[i])}] = {};`);
130132
}
131133
}
132134

@@ -136,15 +138,15 @@ function compile(fields, options, config) {
136138
fieldName = helpers.srcEscape(fields[i].name);
137139
parserFn(`// ${fieldName}: ${typeNames[fields[i].columnType]}`);
138140
if (typeof options.nestTables === 'string') {
139-
lvalue = `this[${helpers.srcEscape(
141+
lvalue = `result[${helpers.srcEscape(
140142
fields[i].table + options.nestTables + fields[i].name
141143
)}]`;
142144
} else if (options.nestTables === true) {
143-
lvalue = `this[${helpers.srcEscape(fields[i].table)}][${fieldName}]`;
145+
lvalue = `result[${helpers.srcEscape(fields[i].table)}][${fieldName}]`;
144146
} else if (options.rowsAsArray) {
145147
lvalue = `result[${i.toString(10)}]`;
146148
} else {
147-
lvalue = `this[${fieldName}]`;
149+
lvalue = `result[${fieldName}]`;
148150
}
149151
const encodingExpr = `CharsetToEncoding[fields[${i}].characterSet]`;
150152
const readCode = readCodeFor(
@@ -167,9 +169,7 @@ function compile(fields, options, config) {
167169
}
168170
}
169171

170-
if (options.rowsAsArray) {
171-
parserFn('return result;');
172-
}
172+
parserFn('return result;');
173173

174174
parserFn('};')('})()');
175175

test/integration/connection/test-multiple-results.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ function do_test(testIndex) {
113113
_numResults = 1;
114114
} else if (_rows.length > 0) {
115115
if (
116-
_rows.constructor.name === 'Array' &&
117-
_rows[0].constructor.name === 'TextRow'
116+
_rows.constructor.name === 'Array'
118117
) {
119118
_numResults = 1;
120119
}

test/unit/commands/test-query.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ const testQuery = new Query({}, (err, res) => {
99
assert.equal(res, null);
1010
});
1111

12-
testQuery._rowParser = class FailingRowParser {
13-
constructor() {
14-
throw testError;
15-
}
16-
};
12+
testQuery._rowParser = function FailingRowParser() {
13+
throw testError;
14+
}
1715

1816
testQuery.row({
1917
isEOF: () => false

0 commit comments

Comments
 (0)