aoa_to_sheet() crashes browser if you pass an array of objects #1214

Closed
opened 2018-08-14 09:26:51 +00:00 by mnori · 2 comments
mnori commented 2018-08-14 09:26:51 +00:00 (Migrated from github.com)

I know you're not supposed to do this, but if you pass an array of objects into aoa_to_sheet() it gets stuck in an infinite loop.

E.g.

XLSX.utils.aoa_to_sheet([{"hello": "world"}]);
// RIP your chrome tab X_X

JSfiddle

Might be good to throw an exception in this situation ;)

I know you're not supposed to do this, but if you pass an array of objects into `aoa_to_sheet()` it gets stuck in an infinite loop. E.g. ``` XLSX.utils.aoa_to_sheet([{"hello": "world"}]); // RIP your chrome tab X_X ``` [JSfiddle](https://jsfiddle.net/j9aswgL8/8/) Might be good to throw an exception in this situation ;)
SheetJSDev commented 2018-08-14 13:05:14 +00:00 (Migrated from github.com)

The function assumes an array of arrays is passed in. The infinite loop comes from the inequality check in https://github.com/SheetJS/js-xlsx/blob/master/bits/27_csfutils.js#L104 -- when you pass an object like {hello: "world"}, length is undefined and the loop doesn't terminate.

The fix is to make a check just before that line:

--- a/bits/27_csfutils.js
+++ b/bits/27_csfutils.js
@@ -101,6 +101,8 @@ function sheet_add_aoa(_ws/*:?Worksheet*/, data/*:AOA*/, opts/*:?any*/)/*:Worksh
                if(_R == -1) range.e.r = _R = _range.e.r + 1;
        }
        for(var R = 0; R != data.length; ++R) {
+               if(!data[R]) continue;
+               if(!Array.isArray(data[R])) throw new Error("aoa_to_sheet expects an array of arrays");
                for(var C = 0; C != data[R].length; ++C) {
                        if(typeof data[R][C] === 'undefined') continue;
                        var cell/*:Cell*/ = ({v: data[R][C] }/*:any*/);

(@mnori feel free to submit this as a PR)

The function assumes an array of arrays is passed in. The infinite loop comes from the inequality check in https://github.com/SheetJS/js-xlsx/blob/master/bits/27_csfutils.js#L104 -- when you pass an object like `{hello: "world"}`, `length` is undefined and the loop doesn't terminate. The fix is to make a check just before that line: ```diff --- a/bits/27_csfutils.js +++ b/bits/27_csfutils.js @@ -101,6 +101,8 @@ function sheet_add_aoa(_ws/*:?Worksheet*/, data/*:AOA*/, opts/*:?any*/)/*:Worksh if(_R == -1) range.e.r = _R = _range.e.r + 1; } for(var R = 0; R != data.length; ++R) { + if(!data[R]) continue; + if(!Array.isArray(data[R])) throw new Error("aoa_to_sheet expects an array of arrays"); for(var C = 0; C != data[R].length; ++C) { if(typeof data[R][C] === 'undefined') continue; var cell/*:Cell*/ = ({v: data[R][C] }/*:any*/); ``` (@mnori feel free to submit this as a PR)
RaviRaaja commented 2021-03-28 09:17:38 +00:00 (Migrated from github.com)

Browser main thread is blocked for 30 mins to process array of array to sheet --> UI froze.. any ways to fix this ?

Browser main thread is blocked for 30 mins to process array of array to sheet --> UI froze.. any ways to fix this ?
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#1214
No description provided.