Fix for empty buffer segfault in native bindings

This commit is contained in:
Brian M. Carlson 2014-11-10 23:29:15 -05:00
parent 587cb0a2c8
commit f088442570
2 changed files with 38 additions and 6 deletions

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();
});
});
});
});