Skip to content

Truly independent web browser

LibWeb: Stop recursing on cycles in CSSUnparsedValue

Andreas Kling32c7d02de250
diff --git a/Libraries/LibWeb/Bindings/Rust/src/css/css_unparsed_value.rs b/Libraries/LibWeb/Bindings/Rust/src/css/css_unparsed_value.rs
index 2017fdfd841..5ed884f106a 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/css/css_unparsed_value.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/css/css_unparsed_value.rs
@@ -6,6 +6,8 @@
//! CSSUnparsedValue, https://drafts.css-houdini.org/css-typed-om-1/#cssunparsedvalue
+use std::collections::HashSet;
+
use ak::Utf16String;
use libjs_rust::api::{GcRefCell, MarkedVec, Trace, Vm};
use libweb_rust::typed_om::{RetainedStyleValue, unresolved_style_value};
@@ -107,7 +109,11 @@ impl CSSUnparsedValue {
Ok(())
}
- fn contains_unparsed_value(&self, needle: &Self) -> bool {
+ fn contains_unparsed_value(&self, needle: &Self, visited: &mut HashSet<*const Self>) -> bool {
+ // NB: A value can contain itself without containing the needle, so each value is only scanned once.
+ if !visited.insert(core::ptr::from_ref(self)) {
+ return false;
+ }
self.tokens.borrow().iter().any(|segment| {
let CSSUnparsedSegment::CSSVariableReferenceValue(variable_reference) = segment else {
return false;
@@ -116,7 +122,7 @@ impl CSSUnparsedValue {
return false;
};
let fallback: &Self = &fallback;
- core::ptr::eq(fallback, needle) || fallback.contains_unparsed_value(needle)
+ core::ptr::eq(fallback, needle) || fallback.contains_unparsed_value(needle, visited)
})
}
@@ -125,7 +131,7 @@ impl CSSUnparsedValue {
// AD-HOC: It's possible for one of the tokens to contain this in its fallback slot, or a similar situation with
// more levels of nesting. To avoid crashing, do a scan for that first and return the empty string.
// Spec issue: https://github.com/w3c/css-houdini-drafts/issues/1158
- if self.contains_unparsed_value(self) {
+ if self.contains_unparsed_value(self, &mut HashSet::new()) {
return Utf16String::default();
}
diff --git a/Tests/LibWeb/Text/expected/css/CSSUnparsedValue-contains-cycle.txt b/Tests/LibWeb/Text/expected/css/CSSUnparsedValue-contains-cycle.txt
new file mode 100644
index 00000000000..c09556a206d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/CSSUnparsedValue-contains-cycle.txt
@@ -0,0 +1,2 @@
+A value that contains itself: ""
+A value that contains such a value: "var(--b,)"
diff --git a/Tests/LibWeb/Text/input/css/CSSUnparsedValue-contains-cycle.html b/Tests/LibWeb/Text/input/css/CSSUnparsedValue-contains-cycle.html
new file mode 100644
index 00000000000..1148919d11a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/CSSUnparsedValue-contains-cycle.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const cycle = new CSSUnparsedValue([]);
+ cycle[0] = new CSSVariableReferenceValue("--a", cycle);
+ println(`A value that contains itself: "${cycle}"`);
+
+ const outer = new CSSUnparsedValue([new CSSVariableReferenceValue("--b", cycle)]);
+ println(`A value that contains such a value: "${outer}"`);
+ });
+</script>