msgpack / msgpack-javascript

@msgpack/msgpack - MessagePack for JavaScript / msgpack.org[JavaScript/TypeScript/ECMA-262]
https://msgpack.org/
ISC License
1.29k stars 161 forks source link

[bug] decode throws RangeError on empty Uint8Array #198

Closed mrl5 closed 2 years ago

mrl5 commented 2 years ago

decode() throws RangeError on empty Uint8Array

steps to reproduce

const { decode } = require("@msgpack/msgpack");

decode(new Uint8Array());

expected result

hmm hard to tell, maybe null or undefined.

decode(encode());

returns null

actual result

Uncaught RangeError: Offset is outside the bounds of the DataView
    at DataView.getUint8 (<anonymous>)
    at Decoder.readU8 (/home/kuba/tmp_/node_modules/@msgpack/msgpack/dist/Decoder.js:532:33)
    at Decoder.readHeadByte (/home/kuba/tmp_/node_modules/@msgpack/msgpack/dist/Decoder.js:425:34)
    at Decoder.doDecodeSync (/home/kuba/tmp_/node_modules/@msgpack/msgpack/dist/Decoder.js:169:35)
    at Decoder.decode (/home/kuba/tmp_/node_modules/@msgpack/msgpack/dist/Decoder.js:89:29)
    at decode (/home/kuba/tmp_/node_modules/@msgpack/msgpack/dist/decode.js:14:20)
mrl5 commented 2 years ago

workaround is to check if length > 0 and then decode()

gfx commented 2 years ago

This is an expected behavior, as JSON.parse("") raises a SyntaxError.

I've clarified the behavior in #209 by adding test cases to make sure this behavior.