Skip to content

Passing a Buffer to the native bindings is not binary safe #980

@cwp

Description

@cwp

The pure-Javascript implementation will accept a Buffer object as a parameter to a query, and this works no matter what the contents of the Buffer. The native bindings, however, appear to expect a C-style string and truncate the data in the Buffer if it contains a zero byte.

Test case:

var pg = require('pg').native
var conString = "postgres:///dev";

pg.connect(conString, function(err, client, done) {
  if(err) {
    return console.error('error fetching client from pool', err);
  }
  const buf1 = new Buffer([1, 2, 3]);
  const buf2 = new Buffer([0, 1, 2]);
  const buf3 = new Buffer([1, 2, 0]);
  const sql = "SELECT $1::bytea AS buf1, $2::bytea AS buf2, $3::bytea AS buf3";
  client.query(sql, [buf1, buf2, buf3], function(err, result) {
    done();
    pg.end();
    if(err) {
      return console.error('error running query', err);
    }
    console.log(result.rows[0].buf1.length, result.rows[0].buf1);
    console.log(result.rows[0].buf2.length, result.rows[0].buf2);
    console.log(result.rows[0].buf3.length, result.rows[0].buf3);
  });
});

With the pure-Javascript implementation, this produces the expected output:

3 <Buffer 01 02 03>
3 <Buffer 00 01 02>
3 <Buffer 01 02 00>

With native bindings, it produces incorrect output:

3 <Buffer 01 02 03>
0 <Buffer >
2 <Buffer 01 02>

I'm currently working around this problem by wrapping Client.query() and converting any parameters that are Buffers into hex-encoded strings, eg. "\x000102". Not sure what the proper fix would be.

> node --version
v5.7.0
> psql --version
psql (PostgreSQL) 9.4.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions