diff --git a/index.js b/index.js index 83b6847..8f901f7 100644 --- a/index.js +++ b/index.js @@ -56,7 +56,7 @@ function bufferizeInt(num) { function _crc32(buf, previous) { buf = ensureBuffer(buf); if (Buffer.isBuffer(previous)) { - previous = previous.readUInt32BE(0); + previous = previous.length >= 4 ? previous.readUInt32BE(0) : 0; } let crc = ~~previous ^ -1; for (var n = 0; n < buf.length; n++) { diff --git a/tests/crc.test.js b/tests/crc.test.js index 326f797..9864dc2 100644 --- a/tests/crc.test.js +++ b/tests/crc.test.js @@ -111,3 +111,10 @@ test('can do unsigned in append mode', function (t) { t.same(crc, expected); t.end(); }); + +test('no readUInt32BE on small previous buffer', function (t) { + var prev = Buffer.from([0xDE, 0xAD, 0xBE]); + var expected = Buffer.from([0x35, 0x24, 0x41, 0xc2]); + t.same(crc32("abc", ), expected); + t.end(); +});