From 12f0133c7faee79ef2579db8353ebc885ad1affe Mon Sep 17 00:00:00 2001 From: Aikar Date: Sat, 16 Apr 2011 18:21:16 -0400 Subject: [PATCH] Applied msgpack fix for Ubuntu. Added msgpack fix for Ubuntu segmentation fault. Removed debug code that was used for debugging the fault. --- examples/wormhole.js | 2 +- lib/wormhole.js | 5 ----- src/wormhole.cc | 17 +++++++---------- support/msgpack/src/unpack.c | 7 ++++--- 4 files changed, 12 insertions(+), 19 deletions(-) diff --git a/examples/wormhole.js b/examples/wormhole.js index 56da083..8c8852c 100644 --- a/examples/wormhole.js +++ b/examples/wormhole.js @@ -33,7 +33,7 @@ var dump = require('sys').inspect; net.createServer(function(conn) { wormhole(conn, function(msg) { console.log("SERVER: Got message: ", dump(msg)); - //conn.write({result: parseInt(msg.input) + 10}); + conn.write({result: parseInt(msg.input) + 10}); }); }).listen(function() { console.log("Server listening"); diff --git a/lib/wormhole.js b/lib/wormhole.js index 392f2a8..db09903 100644 --- a/lib/wormhole.js +++ b/lib/wormhole.js @@ -34,12 +34,7 @@ var wormhole = new Wormhole(); var result; client.on('data', function (data) { - //data = new Buffer([0xa5, 0x68, 0x65, 0x6c, 0x6c, 0x6f]); - // overriding data for debug purposes on segfault - data = new Buffer([0x82, 0xa5, 0x69, 0x6e, 0x70, 0x75, 0x74, 0xcd, 0x01, 0x39, 0xa6, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20]); wormhole.feed(data); - - // Segfaults here on Ubuntu? while ( (result = wormhole.getResult()) !== undefined) { callback(result); } diff --git a/src/wormhole.cc b/src/wormhole.cc index c232130..4a407ae 100644 --- a/src/wormhole.cc +++ b/src/wormhole.cc @@ -39,6 +39,7 @@ class Wormhole : public ObjectWrap { msgpack_unpacker_destroy(&unpacker); msgpack_unpacked_destroy(&result); } + /** * Creates a new Wormhole and wraps it with a V8 object. */ @@ -62,33 +63,29 @@ class Wormhole : public ObjectWrap { Wormhole* wormhole = ObjectWrap::Unwrap(args.This()); msgpack_unpacker* unpacker = &wormhole->unpacker; + if (args.Length() <= 0 || !Buffer::HasInstance(args[0])) { return ThrowException(Exception::TypeError( String::New("First argument must be a Buffer"))); } + Local buf = args[0]->ToObject(); char* data = Buffer::Data(buf); size_t len = Buffer::Length(buf); - /*char realdata[18] = {0x82, 0xa5, 0x69, 0x6e, 0x70, 0x75, 0x74, 0xcd, 0x01, 0x39, 0xa6, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20}; - char* data = (char*) &realdata; - size_t len = sizeof(realdata);*/ - - size_t buffer_capacity = msgpack_unpacker_buffer_capacity(unpacker); - char* destbuf = msgpack_unpacker_buffer(unpacker); - if(!msgpack_unpacker_reserve_buffer(unpacker, len)) { return ThrowException(Exception::Error( String::New("Could not reserve buffer"))); } - if (buffer_capacity < len) { + if (msgpack_unpacker_buffer_capacity(unpacker) < len) { return ThrowException(Exception::Error( String::New("Buffer capacity is less than required length"))); } - memcpy(destbuf, data, len); + memcpy(msgpack_unpacker_buffer(unpacker), data, len); msgpack_unpacker_buffer_consumed(unpacker, len); + return scope.Close(True()); } @@ -97,7 +94,6 @@ class Wormhole : public ObjectWrap { * This is called AFTER feeding, to read out unpacked results. * This method should be continously called until undefined is returned. * - * Segfaults on Ubuntu due to MessagePack library :( * @return V8::Handle */ static Handle GetResult(const Arguments &args) { @@ -112,6 +108,7 @@ class Wormhole : public ObjectWrap { } return scope.Close(Undefined()); } + msgpack_unpacked result; msgpack_unpacker unpacker; }; diff --git a/support/msgpack/src/unpack.c b/support/msgpack/src/unpack.c index 52b834c..3f776ed 100644 --- a/support/msgpack/src/unpack.c +++ b/support/msgpack/src/unpack.c @@ -335,10 +335,11 @@ msgpack_zone* msgpack_unpacker_release_zone(msgpack_unpacker* mpac) return NULL; } - msgpack_zone* old = mpac->z; - mpac->z = r; + msgpack_zone old = *mpac->z; + *mpac->z = *r; + *r = old; - return old; + return r; } void msgpack_unpacker_reset_zone(msgpack_unpacker* mpac)