From f088442570a7259fa315fe36548419af1d8922e2 Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Mon, 10 Nov 2014 23:29:15 -0500 Subject: [PATCH] Fix for empty buffer segfault in native bindings --- src/binding.cc | 16 ++++++++------ test/integration/gh-issues/675-tests.js | 28 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 test/integration/gh-issues/675-tests.js diff --git a/src/binding.cc b/src/binding.cc index c1c93ed..c2757c6 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -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("")); } - paramValues[i] = cHexString; } else { //a paramter was not a string LOG("Parameter not a string or buffer"); diff --git a/test/integration/gh-issues/675-tests.js b/test/integration/gh-issues/675-tests.js new file mode 100644 index 0000000..128afc6 --- /dev/null +++ b/test/integration/gh-issues/675-tests.js @@ -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(); + }); + }); + }); +});