msgpack / msgpack-javascript

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

RangeError: Start offset undefined is outside the bounds of the buffer #215

Closed vera-wcy closed 2 years ago

vera-wcy commented 2 years ago
import { encode, ExtensionCodec } from '@msgpack/msgpack';
...

export function encodeStatsJson(stats: StatsJson) {
  const extensionCodec = new ExtensionCodec();
  const filteredFields = ['name'];
  extensionCodec.register({
    type: 0,
    encode: (object: any) => {
      if (object instanceof Object && !(object instanceof Array)) {
        for (const field of filteredFields) {
          delete object[field];
        }
      }
      return null;
    },
    decode: () => null,
  });

  const encoded = encode(stats, { extensionCodec });
  return Buffer.from(encoded, encoded.byteOffset, encoded.byteLength);
}

image Hi, can you help me with this problem? Use @msgpack/msgpack v2.8.0 encode to report an error, as shown in the figure. The node version used is v12.20.1. how to solve this problem?

gfx commented 2 years ago

Thank you for your report. This seems a bug in this library. Could you give me an input that causes the error?


By the way, I'm not sure it is related to this issue, but you should not mutate the object in an encoder (i.e. delete object[field];). It may lead to unexpected behaviors. I'll explicitly state the behavior in README.md.

If you'd like to mutate an object to be encoded, you can mutate it just before passing it to encode():

export function encodeStatsJson(stats: StatsJson) {
  const filteredFields = ['name'];
  for (const field of filteredFields) {
    delete stats[field];
  }
  const encoded = encode(stats, { extensionCodec });
  return Buffer.from(encoded, encoded.byteOffset, encoded.byteLength);
}
vera-wcy commented 2 years ago

Regarding this issue, I found that upgrading the Node 16 version will not have this error (I used Node v16.16.0). The input about this error is an oversized object that I can't give you, it's about the collection of project webpack stats objects.

gfx commented 2 years ago

I see. Thank you.

That seems odd ... I think this library should work with nodejs v12 because it does not depend on any specific features in the latest nodejs or ECMA-262. I'll personally investigate it with nodejs v12. Closing.