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

Not catchable "RangeError: Maximum callstack size exceeded" error #34

Open
julienbeaussier opened this issue May 12, 2022 · 0 comments
Open

Comments

@julienbeaussier
Copy link

Hello,

We noticed parsing can fail with some very nested objects where the max call stack size is reached, which is kind of expected with a recursive approach. On my end, it start failing around 3500 depth of nesting.

However, it seems the error cannot be always caught by a try..catch, and handled correctly on our side.

Following is an example code that leads to a RangeError: Maximum Call Stack Exceeded error.

'use strict';

const yj = require('yieldable-json');

const count = 10000;
let objArray = new Array(count * 2 + 1);
for (let i = 0; i < count; i++) {
    objArray[i] = `{"foo${i}":`;
}
objArray[count] = '42';
for (let i = count + 1; i < 2 * count + 1; i++) {
    objArray[i] = '}';
}
const obj = objArray.join('');

try {
    yj.parseAsync(obj, null, 1, 20, (err, res) => {
        if (!err) {
            console.log('Success', res);
        } else {
            console.error('Error in callback', err);
        }
    });
} catch (e) {
    console.error('Try..Catch', e);
}

process.on('uncaughtException', (e) => {
    console.error('Uncaught', e);
});

And it's output:

Uncaught RangeError: Maximum call stack size exceeded
    at parseYield (/xxx/node_modules/yieldable-json/yieldable-parser.js:139:24)
    at parseYield (/xxx/node_modules/yieldable-json/yieldable-parser.js:191:26)
    at parseYield.next (<anonymous>)
    at parseYield (/xxx/node_modules/yieldable-json/yieldable-parser.js:196:37)
    at parseYield.next (<anonymous>)
    at parseYield (/xxx/node_modules/yieldable-json/yieldable-parser.js:196:37)
    at parseYield.next (<anonymous>)
    at parseYield (/xxx/node_modules/yieldable-json/yieldable-parser.js:196:37)
    at parseYield.next (<anonymous>)
    at parseYield (/xxx/node_modules/yieldable-json/yieldable-parser.js:196:37)

Keeping the recursive approach, a solution would be to fail gracefully after a given depth is reached, with for example a new ParseError('RangeError: Maximum call stack size exceeded').

If I have missed something that would have let me handle this issue properly, please let me know.

Thank you.

@julienbeaussier julienbeaussier changed the title Not catchable "Maximum callstack size exceeded" error Not catchable "RangeError: Maximum callstack size exceeded" error May 12, 2022
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

1 participant