Skip to content

Commit 341d38f

Browse files
author
Raul Ochoa
committed
Merge pull request brianc#1 from CartoDB/cdb-2.6-row_limit
Fail if pgsql returned row is bigger than limit
2 parents 836a2dc + e41b6a0 commit 341d38f

File tree

6 files changed

+40
-6
lines changed

6 files changed

+40
-6
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: node_js
22
node_js:
3-
- 0.8
43
- "0.10"
54
before_script:
65
- node script/create-test-tables.js pg://[email protected]:5432/postgres
6+
env:
7+
- PGUSER=postgres PGDATABASE=postgres

Makefile

-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ test: test-unit
2323

2424
test-all: jshint test-unit test-integration test-native test-binary
2525

26-
test-travis: test-all upgrade-pg
27-
@make test-all connectionString=postgres://postgres@localhost:5433/postgres
28-
2926
upgrade-pg:
3027
@chmod 755 script/travis-pg-9.2-install.sh
3128
@./script/travis-pg-9.2-install.sh

lib/connection.js

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var EventEmitter = require('events').EventEmitter;
44
var util = require('util');
55

66
var utils = require(__dirname + '/utils');
7+
var defaults = require(__dirname + '/defaults');
78
var Writer = require('buffer-writer');
89

910
var TEXT_MODE = 0;
@@ -347,6 +348,14 @@ Connection.prototype.parseMessage = function() {
347348
//read message length
348349
var length = this.parseInt32(buffer);
349350

351+
if (defaults.maxRowSize !== undefined) {
352+
if (id == 0x44 && buffer.length > defaults.maxRowSize) {
353+
this.stream.emit('error', new Error('Row too large, was ' + buffer.length + ' bytes'));
354+
this.stream.emit('end');
355+
return false;
356+
}
357+
}
358+
350359
if(remaining <= length) {
351360
this.lastBuffer = this.buffer;
352361
//rewind the last 5 bytes we read

lib/defaults.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ var defaults = module.exports = {
3838

3939
client_encoding: "",
4040

41-
ssl: false
41+
ssl: false,
42+
43+
maxRowSize: undefined
4244
};
4345

4446
//parse int8 so you can get your count values as actual numbers

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"semver": "~1.1.4"
2727
},
2828
"scripts": {
29-
"test": "make test-travis connectionString=postgres://postgres@localhost:5432/postgres",
29+
"test": "make test-all connectionString=postgres://postgres@localhost:5432/postgres",
3030
"install": "node-gyp rebuild || (exit 0)"
3131
},
3232
"engines": {

test/integration/client/error-handling-tests.js

+25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var helper = require(__dirname + '/test-helper');
2+
var pg = helper.pg;
23
var util = require('util');
34

45
var createErorrClient = function() {
@@ -179,3 +180,27 @@ test('query receives error on client shutdown', function() {
179180
}));
180181
});
181182

183+
test('query adeheres to maxRowSize', function() {
184+
// not implemented in native library, so it does not make sense to run this test in native mode
185+
if (helper.config.native) {
186+
return true;
187+
}
188+
189+
var client = new Client(helper.config);
190+
191+
client.connect(function() {
192+
var query = 'SELECT 1 as id',
193+
queryRowSize = 60;
194+
195+
test('too large row', function() {
196+
pg.defaults.maxRowSize = queryRowSize - 1;
197+
client.query(query, assert.calls(function(err, res) {
198+
// relax maxRowSize
199+
pg.defaults.maxRowSize = undefined;
200+
201+
assert(err);
202+
assert.equal(err.message, 'Row too large, was ' + queryRowSize + ' bytes');
203+
}));
204+
});
205+
});
206+
});

0 commit comments

Comments
 (0)