Merge pull request #675 from brianc/issues/675

Postgres Native bindings Segfault when querying with empty buffer
This commit is contained in:
Brian C 2014-11-11 09:49:50 -05:00
commit 6cb49b65e6
3 changed files with 40 additions and 6 deletions

View File

@ -3,3 +3,5 @@ node_js:
- "0.10"
before_script:
- node script/create-test-tables.js pg://postgres@127.0.0.1:5432/postgres
env:
- PGUSER=postgres PGDATABASE=postgres

View File

@ -876,13 +876,17 @@ private:
} else if(val->IsNull()) {
paramValues[i] = NULL;
} else if(val->IsObject() && Buffer::HasInstance(val)) {
char *cHexString = MallocCHexString(val->ToObject());
if(!cHexString) {
LOG("ArgToCStringArray: OUT OF MEMORY OR SOMETHING BAD!");
ReleaseCStringArray(paramValues, i-1);
return 0;
if(Buffer::Length(val) > 0) {
char *cHexString = MallocCHexString(val->ToObject());
if(!cHexString) {
LOG("ArgToCStringArray: OUT OF MEMORY OR SOMETHING BAD!");
ReleaseCStringArray(paramValues, i-1);
return 0;
}
paramValues[i] = cHexString;
} else {
paramValues[i] = MallocCString(NanNew<String>(""));
}
paramValues[i] = cHexString;
} else {
//a paramter was not a string
LOG("Parameter not a string or buffer");

View File

@ -0,0 +1,28 @@
var helper = require('../test-helper');
var assert = require('assert');
helper.pg.connect(function(err, client, done) {
if (err) throw err;
var c = 'CREATE TEMP TABLE posts (body TEXT)';
client.query(c, function(err) {
if (err) throw err;
c = 'INSERT INTO posts (body) VALUES ($1) RETURNING *';
var body = new Buffer('foo');
client.query(c, [body], function(err) {
if (err) throw err;
body = new Buffer([]);
client.query(c, [body], function(err, res) {
done();
if (err) throw err;
assert.equal(res.rows[0].body, '')
helper.pg.end();
});
});
});
});