Skip to content

Commit 3b75b9e

Browse files
axgalloawesomekling
authored andcommitted
LibWeb: Avoid division by zero when computing table measures
For malformed tables which only have cells with span greater than 1, the content sizes for row and column aren't initialized to non-zero values. Avoid undefined behavior in such cases, which sometimes show up on Wikipedia.
1 parent 47595b9 commit 3b75b9e

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Viewport <#document> at (0,0) content-size 800x600 children: not-inline
2+
BlockContainer <html> at (0,0) content-size 800x600 [BFC] children: not-inline
3+
BlockContainer <body> at (8,8) content-size 784x23.46875 children: not-inline
4+
TableWrapper <(anonymous)> at (8,8) content-size 6x23.46875 [BFC] children: not-inline
5+
Box <table> at (8,8) content-size 6x23.46875 table-box [TFC] children: not-inline
6+
BlockContainer <(anonymous)> (not painted) children: inline
7+
TextNode <#text>
8+
Box <tbody> at (8,8) content-size 0x19.46875 table-row-group children: not-inline
9+
BlockContainer <(anonymous)> (not painted) children: inline
10+
TextNode <#text>
11+
Box <tr> at (10,10) content-size 0x19.46875 table-row children: not-inline
12+
BlockContainer <(anonymous)> (not painted) children: inline
13+
TextNode <#text>
14+
BlockContainer <td> at (11,11) content-size 0x17.46875 table-cell [BFC] children: inline
15+
line 0 width: 14.265625, height: 17.46875, bottom: 17.46875, baseline: 13.53125
16+
frag 0 from TextNode start: 0, length: 1, rect: [11,11 14.265625x17.46875]
17+
"A"
18+
TextNode <#text>
19+
BlockContainer <(anonymous)> (not painted) children: inline
20+
TextNode <#text>
21+
BlockContainer <(anonymous)> (not painted) children: inline
22+
TextNode <#text>
23+
BlockContainer <(anonymous)> (not painted) children: inline
24+
TextNode <#text>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<table>
2+
<tbody>
3+
<tr>
4+
<td colspan="2">A</td>
5+
</tr>
6+
</tbody>
7+
</table>

Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -495,17 +495,21 @@ void TableFormattingContext::compute_table_measures()
495495
// the product of:
496496
// - the ratio of the max-content size based on cells of span up to N-1 of the column to the baseline max-content size
497497
// - the outer min-content size of the cell minus the baseline max-content size and baseline border spacing, or 0 if this is negative
498-
cell_min_contribution += (rows_or_columns[rc_index].max_size / static_cast<double>(baseline_max_content_size))
499-
* max(CSSPixels(0), cell_min_size<RowOrColumn>(cell) - baseline_max_content_size - baseline_border_spacing);
498+
if (baseline_max_content_size != 0) {
499+
cell_min_contribution += (rows_or_columns[rc_index].max_size / static_cast<double>(baseline_max_content_size))
500+
* max(CSSPixels(0), cell_min_size<RowOrColumn>(cell) - baseline_max_content_size - baseline_border_spacing);
501+
}
500502

501503
// The contribution of the cell is the sum of:
502504
// the max-content size of the column based on cells of span up to N-1
503505
auto cell_max_contribution = rows_or_columns[rc_index].max_size;
504506
// and the product of:
505507
// - the ratio of the max-content size based on cells of span up to N-1 of the column to the baseline max-content size
506508
// - the outer max-content size of the cell minus the baseline max-content size and the baseline border spacing, or 0 if this is negative
507-
cell_max_contribution += (rows_or_columns[rc_index].max_size / static_cast<double>(baseline_max_content_size))
508-
* max(CSSPixels(0), cell_max_size<RowOrColumn>(cell) - baseline_max_content_size - baseline_border_spacing);
509+
if (baseline_max_content_size != 0) {
510+
cell_max_contribution += (rows_or_columns[rc_index].max_size / static_cast<double>(baseline_max_content_size))
511+
* max(CSSPixels(0), cell_max_size<RowOrColumn>(cell) - baseline_max_content_size - baseline_border_spacing);
512+
}
509513
cell_min_contributions_by_rc_index[rc_index].append(cell_min_contribution);
510514
cell_max_contributions_by_rc_index[rc_index].append(cell_max_contribution);
511515
}

0 commit comments

Comments
 (0)