Truly independent web browser
LibWeb: Take null-checked pointer parameters as optional references
Andreas Kling8a9e6d9f91cf
diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rsindex 3d7076ff4ac..0653494d586 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/lib.rs@@ -183,22 +183,19 @@ fn make_handle(tokenizer: HtmlTokenizer) -> *mut RustFfiTokenizerHandle {/// Get the next token from the tokenizer./// Returns true if a token was produced, false if no more tokens.-///-/// # Safety-/// `handle` must be a valid pointer from `rust_html_tokenizer_create`.-/// `out` must be a valid pointer to an RustFfiToken.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_html_tokenizer_next_token(- handle: *mut RustFfiTokenizerHandle,- out: *mut RustFfiToken,+pub extern "C" fn rust_html_tokenizer_next_token(+ handle: Option<&mut RustFfiTokenizerHandle>,+ out: Option<&mut RustFfiToken>,stop_at_insertion_point: bool,cdata_allowed: bool,) -> bool {- if handle.is_null() || out.is_null() {+ let Some(handle) = handle else {return false;- }- let handle = unsafe { &mut *handle };- let out = unsafe { &mut *out };+ };+ let Some(out) = out else {+ return false;+ };// Fast-path text character in the Data state. For ASCII text runs// this skips the whole state-machine function call plus Token@@ -336,15 +333,11 @@ fn next_token_slow(}/// Switch the tokenizer to a new state.-///-/// # Safety-/// `handle` must be a valid pointer from `rust_html_tokenizer_create`.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_html_tokenizer_switch_state(handle: *mut RustFfiTokenizerHandle, state: u8) {- if handle.is_null() {+pub extern "C" fn rust_html_tokenizer_switch_state(handle: Option<&mut RustFfiTokenizerHandle>, state: u8) {+ let Some(handle) = handle else {return;- }- let handle = unsafe { &mut *handle };+ };let Some(state) = State::from_ffi(state) else {return;};@@ -357,21 +350,19 @@ pub unsafe extern "C" fn rust_html_tokenizer_switch_state(handle: *mut RustFfiTo/// `handle` must be valid. `input` must point to `len` valid UTF-16 code units.#[unsafe(no_mangle)]pub unsafe extern "C" fn rust_html_tokenizer_insert_utf16_input(- handle: *mut RustFfiTokenizerHandle,+ handle: Option<&mut RustFfiTokenizerHandle>,input: *const u16,len: usize,) {- if handle.is_null() {+ let Some(handle) = handle else {return;- }+ };let code_units = if input.is_null() || len == 0 {&[]} else {unsafe { std::slice::from_raw_parts(input, len) }};- unsafe { &mut *handle }- .tokenizer- .insert_input_at_insertion_point(code_units);+ handle.tokenizer.insert_input_at_insertion_point(code_units);}/// Insert ASCII input at the current insertion point.@@ -380,21 +371,19 @@ pub unsafe extern "C" fn rust_html_tokenizer_insert_utf16_input(/// `handle` must be valid. `input` must point to `len` ASCII bytes.#[unsafe(no_mangle)]pub unsafe extern "C" fn rust_html_tokenizer_insert_ascii_input(- handle: *mut RustFfiTokenizerHandle,+ handle: Option<&mut RustFfiTokenizerHandle>,input: *const u8,len: usize,) {- if handle.is_null() {+ let Some(handle) = handle else {return;- }+ };let input = if input.is_null() || len == 0 {&[]} else {unsafe { std::slice::from_raw_parts(input, len) }};- unsafe { &mut *handle }- .tokenizer- .insert_ascii_input_at_insertion_point(input);+ handle.tokenizer.insert_ascii_input_at_insertion_point(input);}/// Append UTF-16 input to the tokenizer input stream.@@ -403,19 +392,19 @@ pub unsafe extern "C" fn rust_html_tokenizer_insert_ascii_input(/// `handle` must be valid. `input` must point to `len` valid UTF-16 code units.#[unsafe(no_mangle)]pub unsafe extern "C" fn rust_html_tokenizer_append_utf16_input(- handle: *mut RustFfiTokenizerHandle,+ handle: Option<&mut RustFfiTokenizerHandle>,input: *const u16,len: usize,) {- if handle.is_null() {+ let Some(handle) = handle else {return;- }+ };let code_units = if input.is_null() || len == 0 {&[]} else {unsafe { std::slice::from_raw_parts(input, len) }};- unsafe { &mut *handle }.tokenizer.append_input(code_units);+ handle.tokenizer.append_input(code_units);}/// Append ASCII input to the tokenizer input stream.@@ -424,19 +413,19 @@ pub unsafe extern "C" fn rust_html_tokenizer_append_utf16_input(/// `handle` must be valid. `input` must point to `len` ASCII bytes.#[unsafe(no_mangle)]pub unsafe extern "C" fn rust_html_tokenizer_append_ascii_input(- handle: *mut RustFfiTokenizerHandle,+ handle: Option<&mut RustFfiTokenizerHandle>,input: *const u8,len: usize,) {- if handle.is_null() {+ let Some(handle) = handle else {return;- }+ };let input = if input.is_null() || len == 0 {&[]} else {unsafe { std::slice::from_raw_parts(input, len) }};- unsafe { &mut *handle }.tokenizer.append_ascii_input(input);+ handle.tokenizer.append_ascii_input(input);}/// Get the tokenizer input that has not been consumed yet.@@ -445,14 +434,16 @@ pub unsafe extern "C" fn rust_html_tokenizer_append_ascii_input(/// `handle`, `out_ptr`, and `out_len` must be valid pointers.#[unsafe(no_mangle)]pub unsafe extern "C" fn rust_html_tokenizer_unparsed_input(- handle: *mut RustFfiTokenizerHandle,+ handle: Option<&mut RustFfiTokenizerHandle>,out_ptr: *mut *const u16,out_len: *mut usize,) {- if handle.is_null() || out_ptr.is_null() || out_len.is_null() {+ let Some(handle) = handle else {+ return;+ };+ if out_ptr.is_null() || out_len.is_null() {return;}- let handle = unsafe { &mut *handle };handle.last_unparsed_input = handle.tokenizer.unparsed_input().encode_utf16().collect();unsafe {*out_ptr = handle.last_unparsed_input.as_ptr();@@ -461,117 +452,86 @@ pub unsafe extern "C" fn rust_html_tokenizer_unparsed_input(}/// Compact already-tokenized input after the parser has consumed a chunk.-///-/// # Safety-/// `handle` must be a valid pointer.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_html_tokenizer_parser_did_run(handle: *mut RustFfiTokenizerHandle) {- if handle.is_null() {+pub extern "C" fn rust_html_tokenizer_parser_did_run(handle: Option<&mut RustFfiTokenizerHandle>) {+ let Some(handle) = handle else {return;- }- let handle = unsafe { &mut *handle };+ };handle.tokenizer.parser_did_run();}-/// # Safety-/// `handle` must be a valid pointer.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_html_tokenizer_store_insertion_point(handle: *mut RustFfiTokenizerHandle) {- if handle.is_null() {+pub extern "C" fn rust_html_tokenizer_store_insertion_point(handle: Option<&mut RustFfiTokenizerHandle>) {+ let Some(handle) = handle else {return;- }- let handle = unsafe { &mut *handle };+ };handle.tokenizer.store_insertion_point();}-/// # Safety-/// `handle` must be a valid pointer.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_html_tokenizer_restore_insertion_point(handle: *mut RustFfiTokenizerHandle) {- if handle.is_null() {+pub extern "C" fn rust_html_tokenizer_restore_insertion_point(handle: Option<&mut RustFfiTokenizerHandle>) {+ let Some(handle) = handle else {return;- }- let handle = unsafe { &mut *handle };+ };handle.tokenizer.restore_insertion_point();}-/// # Safety-/// `handle` must be a valid pointer.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_html_tokenizer_update_insertion_point(handle: *mut RustFfiTokenizerHandle) {- if handle.is_null() {+pub extern "C" fn rust_html_tokenizer_update_insertion_point(handle: Option<&mut RustFfiTokenizerHandle>) {+ let Some(handle) = handle else {return;- }- let handle = unsafe { &mut *handle };+ };handle.tokenizer.update_insertion_point();}-/// # Safety-/// `handle` must be a valid pointer.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_html_tokenizer_undefine_insertion_point(handle: *mut RustFfiTokenizerHandle) {- if handle.is_null() {+pub extern "C" fn rust_html_tokenizer_undefine_insertion_point(handle: Option<&mut RustFfiTokenizerHandle>) {+ let Some(handle) = handle else {return;- }- let handle = unsafe { &mut *handle };+ };handle.tokenizer.undefine_insertion_point();}-/// # Safety-/// `handle` must be a valid pointer.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_html_tokenizer_is_insertion_point_defined(handle: *mut RustFfiTokenizerHandle) -> bool {- if handle.is_null() {+pub extern "C" fn rust_html_tokenizer_is_insertion_point_defined(handle: Option<&mut RustFfiTokenizerHandle>) -> bool {+ let Some(handle) = handle else {return false;- }- let handle = unsafe { &mut *handle };+ };handle.tokenizer.is_insertion_point_defined()}-/// # Safety-/// `handle` must be a valid pointer.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_html_tokenizer_is_insertion_point_reached(handle: *mut RustFfiTokenizerHandle) -> bool {- if handle.is_null() {+pub extern "C" fn rust_html_tokenizer_is_insertion_point_reached(handle: Option<&mut RustFfiTokenizerHandle>) -> bool {+ let Some(handle) = handle else {return false;- }- let handle = unsafe { &mut *handle };+ };handle.tokenizer.is_insertion_point_reached()}-/// # Safety-/// `handle` must be a valid pointer.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_html_tokenizer_set_input_stream_closed(- handle: *mut RustFfiTokenizerHandle,+pub extern "C" fn rust_html_tokenizer_set_input_stream_closed(+ handle: Option<&mut RustFfiTokenizerHandle>,closed: bool,) {- if handle.is_null() {+ let Some(handle) = handle else {return;- }- let handle = unsafe { &mut *handle };+ };handle.tokenizer.set_input_stream_closed(closed);}-/// # Safety-/// `handle` must be a valid pointer.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_html_tokenizer_insert_eof(handle: *mut RustFfiTokenizerHandle) {- if handle.is_null() {+pub extern "C" fn rust_html_tokenizer_insert_eof(handle: Option<&mut RustFfiTokenizerHandle>) {+ let Some(handle) = handle else {return;- }- let handle = unsafe { &mut *handle };+ };handle.tokenizer.insert_eof();}-/// # Safety-/// `handle` must be a valid pointer.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_html_tokenizer_abort(handle: *mut RustFfiTokenizerHandle) {- if handle.is_null() {+pub extern "C" fn rust_html_tokenizer_abort(handle: Option<&mut RustFfiTokenizerHandle>) {+ let Some(handle) = handle else {return;- }- let handle = unsafe { &mut *handle };+ };handle.tokenizer.abort();}diff --git a/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs b/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rsindex 62f0be26840..0ce293a40c0 100644--- a/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs+++ b/Libraries/LibWeb/HTML/Parser/Rust/src/parser.rs@@ -5587,7 +5587,7 @@ pub extern "C" fn rust_html_parser_create() -> *mut RustFfiHtmlParserHandle {/// `context_local_name` must be the borrowed identity of a live C++ `Utf16FlyString`.#[unsafe(no_mangle)]pub unsafe extern "C" fn rust_html_parser_begin_fragment(- handle: *mut RustFfiHtmlParserHandle,+ handle: Option<&mut RustFfiHtmlParserHandle>,root: usize,root_insertion_target: usize,context_element: usize,@@ -5601,7 +5601,10 @@ pub unsafe extern "C" fn rust_html_parser_begin_fragment(allow_declarative_shadow_roots: bool,form_element: usize,) {- if handle.is_null() || root == 0 {+ let Some(handle) = handle else {+ return;+ };+ if root == 0 {return;}@@ -5611,7 +5614,6 @@ pub unsafe extern "C" fn rust_html_parser_begin_fragment(};let context_attributes = unsafe { owned_context_attributes_from_ffi(context_attributes, context_attribute_count) };- let handle = unsafe { &mut *handle };handle.state.begin_fragment(FragmentParsingContext {root,root_insertion_target,@@ -5691,37 +5693,31 @@ pub unsafe extern "C" fn rust_html_parser_pop_all_open_elements(handle: *mut Rus/// `handle` must be a valid pointer from `rust_html_parser_create`./// `visitor` must be a valid `GC::Cell::Visitor`.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_html_parser_visit_edges(handle: *const RustFfiHtmlParserHandle, visitor: *mut c_void) {- if handle.is_null() || visitor.is_null() {+pub unsafe extern "C" fn rust_html_parser_visit_edges(handle: Option<&RustFfiHtmlParserHandle>, visitor: *mut c_void) {+ let Some(handle) = handle else {+ return;+ };+ if visitor.is_null() {return;}- let handle = unsafe { &*handle };handle.state.visit_edges(visitor);}/// Take the script element that caused the last Rust HTML parser run to stop.-///-/// # Safety-/// `handle` must be a valid pointer from `rust_html_parser_create`.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_html_parser_take_pending_script(handle: *mut RustFfiHtmlParserHandle) -> usize {- if handle.is_null() {+pub extern "C" fn rust_html_parser_take_pending_script(handle: Option<&mut RustFfiHtmlParserHandle>) -> usize {+ let Some(handle) = handle else {return 0;- }- let handle = unsafe { &mut *handle };+ };handle.state.pending_script.take().unwrap_or(0)}/// Take the SVG script element that caused the last Rust HTML parser run to stop.-///-/// # Safety-/// `handle` must be a valid pointer from `rust_html_parser_create`.#[unsafe(no_mangle)]-pub unsafe extern "C" fn rust_html_parser_take_pending_svg_script(handle: *mut RustFfiHtmlParserHandle) -> usize {- if handle.is_null() {+pub extern "C" fn rust_html_parser_take_pending_svg_script(handle: Option<&mut RustFfiHtmlParserHandle>) -> usize {+ let Some(handle) = handle else {return 0;- }- let handle = unsafe { &mut *handle };+ };handle.state.pending_svg_script.take().unwrap_or(0)}diff --git a/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rs b/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rsindex 39e79f52325..fb898802023 100644--- a/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rs+++ b/Libraries/LibWeb/Rust/src/css/computed_longhand_table.rs@@ -57,7 +57,8 @@ pub(crate) fn longhand_slot_hash(slot: usize, value: *const c_void) -> u64 {if value.is_null() {return mix_slot_hash(slot, 0);}- let content = unsafe { crate::css::style_value::style_value_content_hash(value.cast()) };+ // SAFETY: A non-null slot value is live style value data.+ let content = crate::css::style_value::style_value_content_hash(unsafe { value.cast::<StyleValueData>().as_ref() });mix_slot_hash(slot, content)}@@ -893,6 +894,10 @@ impl ComputedLonghandTable {.map(|(property, value)| (*property, value))}+ pub(crate) fn raw_cascaded_font_size_value(&self) -> Option<&StyleValueData> {+ self.raw_cascaded_font_size.as_ref().map(RetainedStyleValueData::data)+ }+pub(crate) fn raw_cascaded_font_size(&self) -> *const c_void {self.raw_cascaded_font_size.as_ref()diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax.rsindex b6e68bdec00..7ea93aa4af1 100644--- a/Libraries/LibWeb/Rust/src/css/parser/syntax.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax.rs@@ -511,20 +511,22 @@ pub unsafe extern "C" fn rust_syntax_serialize(syntax: *const c_void) -> usize {/// All non-null pointers must remain readable for their accompanying lengths during this call.#[unsafe(no_mangle)]pub unsafe extern "C" fn rust_parse_with_syntax(- context: *const ParseContext,+ context: Option<&ParseContext>,source: FfiUtf16View,syntax: *const c_void,out_status: *mut FfiParseStatus,) -> *const c_void {- if context.is_null() || syntax.is_null() || out_status.is_null() {+ let Some(context) = context else {+ return std::ptr::null();+ };+ if syntax.is_null() || out_status.is_null() {return std::ptr::null();}let Some(source) = (unsafe { source.to_utf16() }) else {unsafe { *out_status = FfiParseStatus::Invalid };return std::ptr::null();};- let Some(parsed) = parse_with_syntax(unsafe { &*context }, &source, unsafe { &*syntax.cast::<SyntaxNode>() })- else {+ let Some(parsed) = parse_with_syntax(context, &source, unsafe { &*syntax.cast::<SyntaxNode>() }) else {unsafe { *out_status = FfiParseStatus::Invalid };return std::ptr::null();};diff --git a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rsindex 585aa1861c0..31b62a2ba75 100644--- a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs@@ -5859,7 +5859,7 @@ pub(crate) enum FontDescriptorKind {/// pointer.#[unsafe(no_mangle)]pub unsafe extern "C" fn rust_parse_css_value(- context: *const ParseContext,+ context: Option<&ParseContext>,property_id: u16,source: FfiUtf16View,out_status: *mut FfiParseStatus,@@ -5871,7 +5871,11 @@ pub unsafe extern "C" fn rust_parse_css_value(std::ptr::null()};- if context.is_null() || out_status.is_null() {+ let Some(context) = context else {+ return invalid_ffi_result();+ };++ if out_status.is_null() {return invalid_ffi_result();}let Some(source) = (unsafe { source.units() }) else {@@ -5879,7 +5883,6 @@ pub unsafe extern "C" fn rust_parse_css_value(};let mut source_utf16 = Vec::with_capacity(source.len());source.append_to(&mut source_utf16);- let context = unsafe { &*context };let outcome = match component_values_from_source(source) {Ok(values) => {let mut substitution_presence = SubstitutionFunctionsPresence::default();@@ -6092,23 +6095,22 @@ fn parse_css_primitive_values(/// All pointers must be valid for their accompanying lengths.#[unsafe(no_mangle)]pub unsafe extern "C" fn rust_parse_entire_css_primitive_from_source(- context: *const ParseContext,+ context: Option<&ParseContext>,value_type: u8,source: FfiUtf16View,range_min: f64,range_max: f64,) -> *const c_void {- if context.is_null() {+ let Some(context) = context else {return std::ptr::null();- }+ };let Some(source) = (unsafe { source.units() }) else {return std::ptr::null();};let Ok(values) = component_values_from_source(source) else {return std::ptr::null();};- let Some((parsed, consumed)) =- parse_css_primitive_values(unsafe { &*context }, value_type, &values, range_min, range_max)+ let Some((parsed, consumed)) = parse_css_primitive_values(context, value_type, &values, range_min, range_max)else {return std::ptr::null();};diff --git a/Libraries/LibWeb/Rust/src/css/style/computed.rs b/Libraries/LibWeb/Rust/src/css/style/computed.rsindex 92a04d12da3..3375ea1256e 100644--- a/Libraries/LibWeb/Rust/src/css/style/computed.rs+++ b/Libraries/LibWeb/Rust/src/css/style/computed.rs@@ -4021,8 +4021,7 @@ fn longhand_table_hash(table: &ComputedLonghandTable) -> u64 {table.publication_sidecars().hash(&mut hasher);table.publication_dependency_flags().hash(&mut hasher);table.pseudo_element_styles().hash(&mut hasher);- unsafe { crate::css::style_value::style_value_content_hash(table.raw_cascaded_font_size().cast()) }- .hash(&mut hasher);+ crate::css::style_value::style_value_content_hash(table.raw_cascaded_font_size_value()).hash(&mut hasher);// NB: Inheritance-dependent values compare independently of insertion order. Sum the// property-mixed hashes, as for longhand slots, without copying and sorting the values.let mut inheritance_dependent_hash_sum = 0_u64;diff --git a/Libraries/LibWeb/Rust/src/css/style/program.rs b/Libraries/LibWeb/Rust/src/css/style/program.rsindex 2cf503ab65c..2e3fb455690 100644--- a/Libraries/LibWeb/Rust/src/css/style/program.rs+++ b/Libraries/LibWeb/Rust/src/css/style/program.rs@@ -333,8 +333,7 @@ fn share_rule_declarations(for values in [&data.written_values, &data.custom_written_values] {values.len().hash(&mut hasher);for value in values {- // SAFETY: The handle retains a live value.- let hash = unsafe { crate::css::style_value::style_value_content_hash(value.pointer()) };+ let hash = crate::css::style_value::style_value_content_hash(value.optional_data());hash.hash(&mut hasher);}}diff --git a/Libraries/LibWeb/Rust/src/css/style_value.rs b/Libraries/LibWeb/Rust/src/css/style_value.rsindex 4d4ee03e186..e4ff7a274f1 100644--- a/Libraries/LibWeb/Rust/src/css/style_value.rs+++ b/Libraries/LibWeb/Rust/src/css/style_value.rs@@ -335,13 +335,11 @@ impl Drop for RetainedComponentValueList {}}-/// # Safety-/// `value` must be null or point to a live `StyleValueData`.-pub(crate) unsafe fn style_value_content_hash(value: *const StyleValueData) -> u64 {- if value.is_null() {+pub(crate) fn style_value_content_hash(value: Option<&StyleValueData>) -> u64 {+ let Some(value) = value else {return 0;- }- unsafe { &*value }.content_hash()+ };+ value.content_hash()}/// A strong reference to immutable Rust-owned style value data.