Truly independent web browser
LibWeb: Clamp the tracks a grid repeat() expands into
Andreas Kling3e898d4dde70
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rsindex bbf62c7bb97..180f516fe64 100644--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs@@ -7017,6 +7017,17 @@ fn definition_for(entry: &ComputedGridTrackEntry, auto_fit: bool, auto_repeat: b}}+// NB: A repeat() copies its line names into every repetition, so a long name list repeated many times would expand into+// a huge number of names. Names past this limit are dropped, as lines past MAX_GRID_LINE_NUMBER are.+const MAX_EXPANDED_LINE_NAMES: usize = 100_000;++/// Appends `names` to `line`, dropping those past the `names_left` an expansion still may make.+fn extend_line_names(line: &mut Vec<LineName>, names: impl Iterator<Item = LineName>, names_left: &mut usize) {+ let length_before = line.len();+ line.extend(names.take(*names_left));+ *names_left -= line.len() - length_before;+}+#[allow(clippy::too_many_arguments)]fn expand_standalone_list<'pass>(source: TrackListSource<'pass>,@@ -7024,17 +7035,23 @@ fn expand_standalone_list<'pass>(lines: &mut Vec<Vec<LineName>>,tracks: &mut Vec<TrackDefinition<'pass>>,pending_names: &mut Vec<LineName>,+ names_left: &mut usize,auto_repeat_count: &mut impl FnMut(u32, &ComputedGridTrackEntry) -> usize,inherited_auto_fit: bool,inherited_auto_repeat: bool,) {source.for_each_entry(list, |entry_index, entry| match entry.kind {kind if kind == ComputedGridTrackEntryKind::LineNames as u8 => {- pending_names.extend(source.names(entry));+ extend_line_names(pending_names, source.names(entry), names_left);}kind if kind == ComputedGridTrackEntryKind::TrackSize as u8|| kind == ComputedGridTrackEntryKind::MinMax as u8 =>{+ // NB: Lines past the limit are dropped (see MAX_GRID_LINE_NUMBER), so a repeat() with a huge count does not+ // expand into more tracks than can be placed into.+ if tracks.len() >= MAX_GRID_LINE_NUMBER as usize {+ return;+ }lines.push(std::mem::take(pending_names));tracks.push(definition_for(entry, inherited_auto_fit, inherited_auto_repeat));}@@ -7047,12 +7064,16 @@ fn expand_standalone_list<'pass>(_ => unreachable!("invalid grid repeat type"),};for _ in 0..repeat_count {+ if tracks.len() >= MAX_GRID_LINE_NUMBER as usize {+ break;+ }expand_standalone_list(source,entry.repeat_list,lines,tracks,pending_names,+ names_left,auto_repeat_count,inherited_auto_fit || is_auto_fit,inherited_auto_repeat || is_auto_repeat,@@ -7085,12 +7106,14 @@ pub(crate) fn expand_standalone(let mut result = ExpandedTrackList::default();let mut pending_names = Vec::new();+ let mut names_left = MAX_EXPANDED_LINE_NAMES;expand_standalone_list(source,list,&mut result.lines,&mut result.tracks,&mut pending_names,+ &mut names_left,&mut auto_repeat_count,false,false,@@ -7125,6 +7148,7 @@ fn expand_subgrid_names(list: ComputedGridTrackList,lines: &mut [Vec<LineName>],line_index: &mut usize,+ names_left: &mut usize,) {let entries = {let mut entries: Vec<(u32, &ComputedGridTrackEntry)> = Vec::new();@@ -7136,7 +7160,7 @@ fn expand_subgrid_names(match entry.kind {kind if kind == ComputedGridTrackEntryKind::LineNames as u8 => {if let Some(line) = lines.get_mut(*line_index) {- line.extend(source.names(entry));+ extend_line_names(line, source.names(entry), names_left);}*line_index += 1;}@@ -7175,7 +7199,10 @@ fn expand_subgrid_names(_ => unreachable!("invalid grid repeat type"),};for _ in 0..repeat_count {- expand_subgrid_names(source, entry.repeat_list, lines, line_index);+ if *line_index >= lines.len() {+ break;+ }+ expand_subgrid_names(source, entry.repeat_list, lines, line_index, names_left);}}_ => {}@@ -7209,7 +7236,8 @@ pub(crate) fn expand_subgrid<'pass>(}));}let mut line_index = 0;- expand_subgrid_names(source, list, &mut lines, &mut line_index);+ let mut names_left = MAX_EXPANDED_LINE_NAMES;+ expand_subgrid_names(source, list, &mut lines, &mut line_index, &mut names_left);ExpandedTrackList {lines,tracks: Vec::new(),diff --git a/Tests/LibWeb/Crash/CSS/grid-repeat-with-huge-count.html b/Tests/LibWeb/Crash/CSS/grid-repeat-with-huge-count.htmlnew file mode 100644index 00000000000..9f290f7b9f3--- /dev/null+++ b/Tests/LibWeb/Crash/CSS/grid-repeat-with-huge-count.html@@ -0,0 +1,26 @@+<!DOCTYPE html>+<!-- A repeat() with a huge count, or a long line name list repeated many times, must not expand into that many tracks,+ line name lists or line names. -->+<style>+ .grid {+ display: grid;+ grid-template-rows: repeat(2147483647, auto);+ grid-template-columns: repeat(2147483647, 10px);+ }+ .subgrid {+ display: grid;+ grid-column: span 2;+ grid-template-columns: subgrid repeat(2147483647, [a]);+ }+</style>+<div class="grid"><div>a</div><div>b</div></div>+<div class="grid"><div class="subgrid"><div>c</div></div></div>+<div id="names"><div>d</div></div>+<div id="subgrid-names-parent"><div id="subgrid-names"><div>e</div></div></div>+<script>+ const names = Array.from({ length: 50000 }, (_, i) => `n${i}`).join(" ");+ document.getElementById("names").style.cssText = `display: grid; grid-template-columns: repeat(10000, [${names}] 1px)`;+ document.getElementById("subgrid-names-parent").style.cssText = "display: grid; grid-template-columns: repeat(10000, 1px)";+ document.getElementById("subgrid-names").style.cssText = `display: grid; grid-column: span 10000; grid-template-columns: subgrid repeat(10000, [${names}])`;+ document.body.offsetWidth;+</script>