From 9d3ed1bb9231798df8a081746fd0dc75900d8276 Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 31 Jul 2009 19:16:08 +0200 Subject: [PATCH] Bugfix: negative integers in raw encoding stream. Add test. Reported by Tim Caswell. --- src/net.cc | 2 +- test/mjsunit/test-tcp-raw.js | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 test/mjsunit/test-tcp-raw.js diff --git a/src/net.cc b/src/net.cc index c11a5392d2..6a254ec8b8 100644 --- a/src/net.cc +++ b/src/net.cc @@ -437,7 +437,7 @@ Connection::OnReceive (const void *buf, size_t len) // raw encoding Local array = Array::New(len); for (size_t i = 0; i < len; i++) { - char val = static_cast(buf)[i]; + unsigned char val = static_cast(buf)[i]; array->Set(Integer::New(i), Integer::New(val)); } argv[0] = array; diff --git a/test/mjsunit/test-tcp-raw.js b/test/mjsunit/test-tcp-raw.js new file mode 100644 index 0000000000..0acfc8e180 --- /dev/null +++ b/test/mjsunit/test-tcp-raw.js @@ -0,0 +1,47 @@ +include("mjsunit.js"); +PORT = 23123; + +var echoServer = node.tcp.createServer(function (connection) { + connection.addListener("receive", function (chunk) { + connection.send(chunk, "raw"); + }); + connection.addListener("eof", function () { + connection.close(); + }); +}); +echoServer.listen(PORT); + +var recv = []; +var j = 0; + +function onLoad () { + var c = node.tcp.createConnection(PORT); + + c.addListener("receive", function (chunk) { + if (++j < 256) { + c.send([j], "raw"); + } else { + c.close(); + } + for (var i = 0; i < chunk.length; i++) { + recv.push(chunk[i]); + } + }); + + c.addListener("connect", function () { + c.send([j], "raw"); + }); + + c.addListener("disconnect", function () { + p(recv); + echoServer.close(); + }); +}; + +function onExit () { + var expected = []; + for (var i = 0; i < 256; i++) { + expected.push(i); + } + assertEquals(expected, recv); +}