Skip to content

Avoid infinite loop and fatal errors for invalid table data #296

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/layout-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,19 @@ const { ColSpanCell, RowSpanCell } = Cell;
}

function addRowSpanCells(table) {
table.forEach(function (row, rowIndex) {
table.forEach(function (row) {
row.forEach(function (cell) {
for (let i = 1; i < cell.rowSpan; i++) {
let rowSpanCell = new RowSpanCell(cell);
const line = cell.y + i;
rowSpanCell.x = cell.x;
rowSpanCell.y = cell.y + i;
rowSpanCell.y = line;
rowSpanCell.colSpan = cell.colSpan;
insertCell(rowSpanCell, table[rowIndex + i]);
if (!table[line]) {
const { x, y } = rowSpanCell;
warn(`${x}-${y}: Expected empty array for row ${line - 1} (line ${line}).`);
}
insertCell(rowSpanCell, table[rowSpanCell.y] || []);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phantom row will never be output. But a fatal error is avoided.

}
});
});
Expand All @@ -110,7 +115,7 @@ const { ColSpanCell, RowSpanCell } = Cell;
let colSpanCell = new ColSpanCell();
colSpanCell.x = cell.x + k;
colSpanCell.y = cell.y;
cellColumns.splice(columnIndex + 1, 0, colSpanCell);
cellColumns.splice(colSpanCell.x, 0, colSpanCell);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the (known) infinite loop that can be caused by invalid-table-data.

}
}
}
Expand Down Expand Up @@ -146,7 +151,7 @@ const { ColSpanCell, RowSpanCell } = Cell;
cell.x = opts.x;
cell.y = opts.y;
warn(`Missing cell at ${cell.y}-${cell.x}.`);
insertCell(cell, table[y]);
insertCell(cell, table[y] || []);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phantom row will never be output. But a fatal error is avoided.

}
}
}
Expand Down
33 changes: 33 additions & 0 deletions test/issues/296-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const Table = require('../..');

/**
* This test doesn't actually fail it never completes. I could not find a method
* to get the infinite loop to timeout within the test. Not sure how GHA will
* handle this.
*/
test('it should not loop infinitely with invalid table data', async () => {
const table = new Table();
table.push(
[
{ content: 'A', colSpan: 2 },
{ content: 'B', rowSpan: 3 },
],
[],
[{ content: 'C', colSpan: 3 }]
);
expect(() => table.toString()).not.toThrow();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expectation induces an infinite loop before the "fix" in this pr.

});

test('it should not error on invalid table data', () => {
const table = new Table({ debug: true });
table.push(
[
{ content: 'A', colSpan: 2 },
{ content: 'B', rowSpan: 3 },
],
[{ content: 'C', colSpan: 3 }]
);
expect(() => table.toString()).not.toThrow();
// This expectation can be dropped if the expectation in the code changes
expect(table.messages).toContain('2-2: Expected empty array for row 1 (line 2).');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silent solution to the missing row. Future solutions can add the missing row to correct the output.

});