Truly independent web browser
LibURL: Pass a port across FFI in 32 bits
Andreas Kling8d811d75a433
diff --git a/Libraries/LibURL/Rust/src/ffi/url.rs b/Libraries/LibURL/Rust/src/ffi/url.rsindex b462f517b98..5b12e593177 100644--- a/Libraries/LibURL/Rust/src/ffi/url.rs+++ b/Libraries/LibURL/Rust/src/ffi/url.rs@@ -244,10 +244,13 @@ pub unsafe extern "C" fn rust_url_set_password(pub unsafe extern "C" fn rust_url_set_port(url: *const RustUrl,has_port: bool,- port: u16,+ port: u32,ctx: *mut c_void,on_complete: FfiUrlResultFn,) {+ // NB: A port travels in 32 bits. rustc takes the upper bits of a narrower parameter to be zero, which GCC 16 does not+ // hold to: it sign-extends a u16 of 32768 or above.+ let port = port as u16;abort_on_panic(|| unsafe {modify_url(url, ctx, on_complete, |url| url.set_port(has_port.then_some(port)));})diff --git a/Tests/LibURL/TestURL.cpp b/Tests/LibURL/TestURL.cppindex e970eb67679..a7f7191708d 100644--- a/Tests/LibURL/TestURL.cpp+++ b/Tests/LibURL/TestURL.cpp@@ -96,6 +96,19 @@ TEST_CASE(basic)}}+TEST_CASE(set_port_of_every_magnitude)+{+ auto url = URL::Parser::basic_parse("http://example.com/"sv);+ VERIFY(url.has_value());+ for (auto port : { 8080, 32767, 32768, 40000, 65535 }) {+ url->set_port(port);+ EXPECT_EQ(url->port_or_default(), port);+ EXPECT_EQ(url->serialize(), MUST(String::formatted("http://example.com:{}/", port)));+ }+ url->set_port({});+ EXPECT_EQ(url->serialize(), "http://example.com/"sv);+}+TEST_CASE(some_bad_urls){EXPECT_EQ(URL::Parser::basic_parse("http//serenityos.org"sv).has_value(), false);