Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught RangeError: Maximum call stack size exceeded on decode #800

Closed
apankov opened this issue May 22, 2017 · 1 comment
Closed

Uncaught RangeError: Maximum call stack size exceeded on decode #800

apankov opened this issue May 22, 2017 · 1 comment

Comments

@apankov
Copy link

apankov commented May 22, 2017

protobuf.js version: 6.7.3

Unable to decode big message.

const protobuf = require("protobufjs");

const proto = {
  "nested": {
    "AwesomeMessage": {
      "fields": {
        "awesomeField": {
          "type": "bytes",
          "id": 1
        }
      }
    }
  }
};

var root = protobuf.Root.fromJSON(proto);

var AwesomeMessage = root.lookupType("AwesomeMessage");
var buff = Buffer.alloc(10*1024*1024, 'a');

var payload = { awesomeField: buff };

var errMsg = AwesomeMessage.verify(payload);

var message = AwesomeMessage.create(payload);

var object = AwesomeMessage.toObject(message, {
   longs: String,
    enums: String,
    bytes: String,
});
/Users/andrey/ttt/node_modules/@protobufjs/base64/index.js:73
    return String.fromCharCode.apply(String, string);
                               ^

RangeError: Maximum call stack size exceeded
    at Object.encode (/Users/andrey/ttt/node_modules/@protobufjs/base64/index.js:73:32)
    at Type._AwesomeMessage$toObject [as toObject] (eval at eof (/Users/andrey/ttt/node_modules/@protobufjs/codegen/index.js:103:25), <anonymous>:11:50)
    at Object.<anonymous> (/Users/andrey/ttt/1.js:27:29)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:393:7)

Quick and dirty fix could be something like:

- return String.fromCharCode.apply(String, string);
+ var enc = require("text-encoding")
+ var encoder = new enc.TextEncoder();
+ return encoder.encode(string)
@dcodeIO
Copy link
Member

dcodeIO commented May 22, 2017

Clearly, the base64 module would benefit from the same chunking mechanism the utf8 module is already using.

See: https://github.com/dcodeIO/protobuf.js/blob/master/lib/utf8/index.js#L60 vs https://github.com/dcodeIO/protobuf.js/blob/master/lib/base64/index.js#L73

@dcodeIO dcodeIO closed this as completed in 66d149e Jun 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants