From 0c777fafec95131f6378d1ef59e1295e8afaca63 Mon Sep 17 00:00:00 2001 From: brianc Date: Mon, 1 Nov 2010 22:11:40 -0400 Subject: [PATCH] fix for strang \0 buffer encoding issue in node v0.3.0 --- lib/connection.js | 8 +++----- lib/writer.js | 6 +++++- test/buffer-list.js | 8 ++++++-- test/unit/connection/inbound-parser-tests.js | 8 ++++---- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index b0344d34..9788e9b8 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -74,7 +74,7 @@ p.startup = function(config) { p.password = function(password) { //0x70 = 'p' - this.send(0x70, Buffer(password + '\0', this.encoding)); + this.send(0x70, new Writer().addCString(password).join()); }; p.send = function(code, bodyBuffer) { @@ -97,7 +97,7 @@ p.end = function() { p.query = function(text) { //0x51 = Q - this.send(0x51, new Buffer(text + '\0', this.encoding)); + this.send(0x51, new Writer().addCString(text).join()); }; p.parse = function(query) { @@ -181,9 +181,7 @@ p.end = function() { }; p.describe = function(msg) { - var str = msg.type + (msg.name || "" ) + '\0'; - var buffer = Buffer(str, this.encoding); - this.send(0x44, buffer); + this.send(0x44, new Writer().addCString(msg.type + (msg.name || '')).join()); }; //parsing methods diff --git a/lib/writer.js b/lib/writer.js index 6d21cb56..cd728079 100644 --- a/lib/writer.js +++ b/lib/writer.js @@ -32,7 +32,11 @@ p.addInt32 = function(val, first) { }; p.addCString = function(val) { - return this.add(Buffer(val + '\0','utf8')); + var len = Buffer.byteLength(val); + var buffer = new Buffer(len+1); + buffer.write(val); + buffer[len] = 0; + return this.add(buffer); }; p.addChar = function(char, first) { diff --git a/test/buffer-list.js b/test/buffer-list.js index d27a0bea..3aa24552 100644 --- a/test/buffer-list.js +++ b/test/buffer-list.js @@ -27,8 +27,12 @@ p.addInt32 = function(val, first) { ]),first); }; -p.addCString = function(val) { - return this.add(Buffer(val + '\0','utf8')); +p.addCString = function(val, front) { + var len = Buffer.byteLength(val); + var buffer = new Buffer(len+1); + buffer.write(val); + buffer[len] = 0; + return this.add(buffer, front); }; p.addChar = function(char, first) { diff --git a/test/unit/connection/inbound-parser-tests.js b/test/unit/connection/inbound-parser-tests.js index 48dc1360..1d12f22d 100644 --- a/test/unit/connection/inbound-parser-tests.js +++ b/test/unit/connection/inbound-parser-tests.js @@ -57,7 +57,7 @@ var oneFieldBuf = new BufferList() .addCString('test') .join(true, 'D'); -var oneFieldBuf = buffers.dataRow(['test\0']); +var oneFieldBuf = buffers.dataRow(['test']); var expectedAuthenticationOkayMessage = { @@ -67,9 +67,9 @@ var expectedAuthenticationOkayMessage = { var expectedParameterStatusMessage = { name: 'parameterStatus', - length: 25, parameterName: 'client_encoding', - parameterValue: 'UTF8' + parameterValue: 'UTF8', + length: 25 }; var expectedBackendKeyDataMessage = { @@ -246,7 +246,7 @@ test('Connection', function() { }); test('field is correct', function() { - assert.equal(message.fields[0],'test\0'); + assert.equal(message.fields[0],'test'); }); });