Skip to content

Commit

Permalink
Applied msgpack fix for Ubuntu.
Browse files Browse the repository at this point in the history
Added msgpack fix for Ubuntu segmentation fault.
Removed debug code that was used for debugging the fault.
  • Loading branch information
aikar committed Apr 16, 2011
1 parent d901ddd commit 12f0133
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/wormhole.js
Expand Up @@ -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");
Expand Down
5 changes: 0 additions & 5 deletions lib/wormhole.js
Expand Up @@ -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);
}
Expand Down
17 changes: 7 additions & 10 deletions src/wormhole.cc
Expand Up @@ -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.
*/
Expand All @@ -62,33 +63,29 @@ class Wormhole : public ObjectWrap {
Wormhole* wormhole = ObjectWrap::Unwrap<Wormhole>(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<Object> 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());
}

Expand All @@ -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<Value>
*/
static Handle<Value> GetResult(const Arguments &args) {
Expand All @@ -112,6 +108,7 @@ class Wormhole : public ObjectWrap {
}
return scope.Close(Undefined());
}

msgpack_unpacked result;
msgpack_unpacker unpacker;
};
Expand Down
7 changes: 4 additions & 3 deletions support/msgpack/src/unpack.c
Expand Up @@ -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)
Expand Down

0 comments on commit 12f0133

Please sign in to comment.