pierrec / node-lz4

LZ4 fast compression algorithm for NodeJS
MIT License
438 stars 98 forks source link

encode() bombs with TypeError #7

Closed ccstolley closed 9 years ago

ccstolley commented 10 years ago

This seems like it should work:

node -e 'require("lz4").encode(new Buffer("crap"));'

But it yields:

buffer.js:784
    throw TypeError('value is out of bounds');
          ^
TypeError: value is out of bounds
    at TypeError (<anonymous>)
    at checkInt (buffer.js:784:11)
    at Buffer.writeUInt32LE (buffer.js:841:5)
    at LZ4Stream._add (/home/stolley/viaprotect-web/www/api/node_modules/lz4/lib/encoder.js:95:13)
    at LZ4Stream.add (/home/stolley/viaprotect-web/www/api/node_modules/lz4/lib/encoder.js:73:8)
    at Object.exports.LZ4_compress [as encode] (/home/stolley/viaprotect-web/www/api/node_modules/lz4/lib/encoder.js:185:7)
    at [eval]:1:16
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (module.js:456:26)
    at evalScript (node.js:532:25)

This is apparently due to a bad bitwise OR statement at line 95 in encoder.js that produces a signed int rather than unsigned, which in turn blows an exception in buffer.js:

    blockSize.writeUInt32LE( 0x80000000 | data.length, 0, false)

However, replacing the bitwise OR with + does not fix the issue completely. When attempting to decode, you will hit a different error:

node -e 'var lz4 = require("lz4"); lz4.decode(lz4.encode(new Buffer("crap")));'

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: Unexpected end of LZ4 stream @0
    at Decoder.emit_Error (/home/stolley/viaprotect-web/www/api/node_modules/lz4/lib/decoder_stream.js:75:22)
    at Decoder.check_Size (/home/stolley/viaprotect-web/www/api/node_modules/lz4/lib/decoder_stream.js:81:32)
    at Decoder.read_DataBlockData (/home/stolley/viaprotect-web/www/api/node_modules/lz4/lib/decoder_stream.js:218:12)
    at Decoder._main (/home/stolley/viaprotect-web/www/api/node_modules/lz4/lib/decoder_stream.js:314:25)
    at Decoder._flush (/home/stolley/viaprotect-web/www/api/node_modules/lz4/lib/decoder_stream.js:284:7)
    at Decoder.<anonymous> (_stream_transform.js:130:12)
    at Decoder.g (events.js:175:14)
    at Decoder.EventEmitter.emit (events.js:92:17)
    at finishMaybe (_stream_writable.js:354:12)
    at endWritable (_stream_writable.js:361:3)
pierrec commented 10 years ago

Hello Colin,

Thanks for reporting and all the details! I have reproduced and fixed your issue. It had to do with the encoder/decoder not handling uncompressable data well.

You should use v0.2.3 to get the latest fixes.

Cheers,

Pierre

valeeum commented 10 years ago

I'm still getting a similar error when trying to call .encode() on a string using version 0.3.11. Any idea what my be causing this issue?

buffer.js:705 throw TypeError('value is out of bounds'); ^ TypeError: value is out of bounds at TypeError () at checkInt (buffer.js:705:11) at Buffer.writeInt32LE (buffer.js:782:5) at Encoder._flush (/var/www/node_modules/lz4/lib/encoder_stream.js:210:7) at Encoder. (_stream_transform.js:130:12) at Encoder.g (events.js:180:16) at Encoder.emit (events.js:92:17) at finishMaybe (_stream_writable.js:359:12) at endWritable (_stream_writable.js:366:3) at Encoder.Writable.end (_stream_writable.js:344:5)

pierrec commented 10 years ago

I have pushed an update (0.3.12). Let me know if this resolves your issue.