Skip to content

Commit

Permalink
throwing on failed allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
bobrik committed Feb 8, 2014
1 parent c3abe9a commit 41f95e8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
57 changes: 30 additions & 27 deletions src/lzf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,31 @@ using namespace node;


Handle<Value> ThrowNodeError(const char* what = NULL) {
return ThrowException(Exception::Error(String::New(what)));
return ThrowException(Exception::Error(String::New(what)));
}

Handle<Value> compress(const Arguments& args) {
if (args.Length() < 1 || !Buffer::HasInstance(args[0])) {
return ThrowNodeError("First argument must be a Buffer");
}
if (args.Length() < 1 || !Buffer::HasInstance(args[0])) {
return ThrowNodeError("First argument must be a Buffer");
}

HandleScope scope;
HandleScope scope;

Local<Object> bufferIn = args[0]->ToObject();
Local<Object> bufferIn = args[0]->ToObject();
size_t bytesIn = Buffer::Length(bufferIn);
char * dataPointer = Buffer::Data(bufferIn);
size_t bytesCompressed = bytesIn + 100;
char * bufferOut = (char*) malloc(bytesCompressed);
char * bufferOut = (char*) malloc(bytesCompressed);

if (!bufferOut) {
return ThrowNodeError("LZF malloc failed!");
}

unsigned result = lzf_compress(dataPointer, bytesIn, bufferOut, bytesCompressed);

if (!result) {
free(bufferOut);
return Undefined();
return ThrowNodeError("Compression failed, probably too small buffer");
}

Buffer *BufferOut = Buffer::New(bufferOut, result);
Expand All @@ -47,43 +51,42 @@ Handle<Value> compress(const Arguments& args) {


Handle<Value> decompress(const Arguments &args) {
if (args.Length() < 1 || !Buffer::HasInstance(args[0])) {
return ThrowNodeError("First argument must be a Buffer");
}
if (args.Length() < 1 || !Buffer::HasInstance(args[0])) {
return ThrowNodeError("First argument must be a Buffer");
}

Local<Object> bufferIn = args[0]->ToObject();
Local<Object> bufferIn = args[0]->ToObject();

size_t bytesUncompressed = 999 * 1024 * 1024; // it's about max size that V8 supports

if (args.Length() > 1 && args[1]->IsNumber()) { // accept dest buffer size
bytesUncompressed = args[1]->Uint32Value();
}
size_t bytesUncompressed = 999 * 1024 * 1024; // it's about max size that V8 supports

if (args.Length() > 1 && args[1]->IsNumber()) { // accept dest buffer size
bytesUncompressed = args[1]->Uint32Value();
}

char * bufferOut = (char*) malloc(bytesUncompressed);
if (!bufferOut) {
return ThrowNodeError("LZF malloc failed!");
}

char * bufferOut = (char*) malloc(bytesUncompressed);
if (!bufferOut) {
return ThrowNodeError("LZF malloc failed!");
}

unsigned result = lzf_decompress(Buffer::Data(bufferIn), Buffer::Length(bufferIn), bufferOut, bytesUncompressed);

if (!result) {
return Undefined();
return ThrowNodeError("Unrompression failed, probably too small buffer");
}

Buffer * BufferOut = Buffer::New(bufferOut, result);

free(bufferOut);

HandleScope scope;
return scope.Close(BufferOut->handle_);
HandleScope scope;
return scope.Close(BufferOut->handle_);
}

extern "C" void
init (Handle<Object> target) {
NODE_SET_METHOD(target, "compress", compress);
NODE_SET_METHOD(target, "decompress", decompress);
NODE_SET_METHOD(target, "compress", compress);
NODE_SET_METHOD(target, "decompress", decompress);
}

NODE_MODULE(lzf, init)

12 changes: 11 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ var loremBuffer = new Buffer(lorem);
var loremCompressed = lzf.compress(loremBuffer);
var loremDecompressed = lzf.decompress(loremCompressed);

// check default output buffer
assert.equal(loremDecompressed.toString(), lorem);
console.log("test ok");

// check minimum output buffer
assert.equal(lzf.decompress(loremCompressed, loremBuffer.length), lorem);

// check error on too small buffer
try {
lzf.decompress(loremCompressed, loremBuffer.length - 1);
assert.fail("exception should be thrown for too small buffer");
} catch (e) {}

console.log("test ok");

0 comments on commit 41f95e8

Please sign in to comment.