From 4cba8fb8e614eeaa4bdd38f14f56f665819b012e Mon Sep 17 00:00:00 2001 From: undef1nd Date: Sun, 15 Mar 2020 12:09:57 +0000 Subject: [PATCH] Bug 1620769 - Convert net_IsValidScheme function to Rust r=valentin Differential Revision: https://phabricator.services.mozilla.com/D66871 --HG-- extra : moz-landing-system : lando --- netwerk/base/nsURLHelper.cpp | 14 ++------------ netwerk/base/nsURLHelper.h | 6 +----- netwerk/base/nsURLParsers.cpp | 3 ++- netwerk/base/rust-helper/src/helper.h | 2 ++ netwerk/base/rust-helper/src/lib.rs | 24 ++++++++++++++++++++++-- 5 files changed, 29 insertions(+), 20 deletions(-) diff --git a/netwerk/base/nsURLHelper.cpp b/netwerk/base/nsURLHelper.cpp index 9e4f42675929..0a98241fb08d 100644 --- a/netwerk/base/nsURLHelper.cpp +++ b/netwerk/base/nsURLHelper.cpp @@ -453,18 +453,8 @@ nsresult net_ExtractURLScheme(const nsACString& inURI, nsACString& scheme) { return NS_OK; } -bool net_IsValidScheme(const char* scheme, uint32_t schemeLen) { - // first char must be alpha - if (!IsAsciiAlpha(*scheme)) return false; - - // nsCStrings may have embedded nulls -- reject those too - for (; schemeLen; ++scheme, --schemeLen) { - if (!(IsAsciiAlpha(*scheme) || IsAsciiDigit(*scheme) || *scheme == '+' || - *scheme == '.' || *scheme == '-')) - return false; - } - - return true; +bool net_IsValidScheme(const nsACString& scheme) { + return rust_net_is_valid_scheme(scheme); } bool net_IsAbsoluteURL(const nsACString& uri) { diff --git a/netwerk/base/nsURLHelper.h b/netwerk/base/nsURLHelper.h index ebf6e3ea4dc4..15d43302fb1b 100644 --- a/netwerk/base/nsURLHelper.h +++ b/netwerk/base/nsURLHelper.h @@ -94,11 +94,7 @@ bool net_IsAbsoluteURL(const nsACString& inURL); nsresult net_ExtractURLScheme(const nsACString& inURI, nsACString& scheme); /* check that the given scheme conforms to RFC 2396 */ -bool net_IsValidScheme(const char* scheme, uint32_t schemeLen); - -inline bool net_IsValidScheme(const nsCString& scheme) { - return net_IsValidScheme(scheme.get(), scheme.Length()); -} +bool net_IsValidScheme(const nsACString& scheme); /** * This function strips out all C0 controls and space at the beginning and end diff --git a/netwerk/base/nsURLParsers.cpp b/netwerk/base/nsURLParsers.cpp index 3963f5b3657e..2ed642230d8d 100644 --- a/netwerk/base/nsURLParsers.cpp +++ b/netwerk/base/nsURLParsers.cpp @@ -112,7 +112,8 @@ nsBaseURLParser::ParseURL(const char* spec, int32_t specLen, // spec = : // spec = : // - if (!net_IsValidScheme(spec, colon - spec) || (*(colon + 1) == ':')) { + if (!net_IsValidScheme(nsDependentCSubstring(spec, colon - spec)) || + (*(colon + 1) == ':')) { return NS_ERROR_MALFORMED_URI; } SET_RESULT(scheme, offset, colon - spec); diff --git a/netwerk/base/rust-helper/src/helper.h b/netwerk/base/rust-helper/src/helper.h index e182c3f1bb67..a1ac7c9a6be0 100644 --- a/netwerk/base/rust-helper/src/helper.h +++ b/netwerk/base/rust-helper/src/helper.h @@ -20,6 +20,8 @@ bool rust_net_is_valid_ipv4_addr(const nsACString& aAddr); bool rust_net_is_valid_ipv6_addr(const nsACString& aAddr); bool rust_net_is_valid_scheme_char(const char aChar); + +bool rust_net_is_valid_scheme(const nsACString& scheme); } #endif // RUST_NS_NET_HELPER diff --git a/netwerk/base/rust-helper/src/lib.rs b/netwerk/base/rust-helper/src/lib.rs index 64c49c54d07d..de3baac4efce 100644 --- a/netwerk/base/rust-helper/src/lib.rs +++ b/netwerk/base/rust-helper/src/lib.rs @@ -286,6 +286,26 @@ pub fn is_valid_ipv6_addr<'a>(addr: &'a [u8]) -> bool { #[no_mangle] #[allow(non_snake_case)] pub extern "C" fn rust_net_is_valid_scheme_char(a_char: u8) -> bool { - let a_char = a_char as char; - a_char.is_ascii_alphanumeric() || a_char == '+' || a_char == '.' || a_char == '-' + is_valid_scheme_char(a_char) +} + +#[no_mangle] +#[allow(non_snake_case)] +pub extern "C" fn rust_net_is_valid_scheme<'a>(scheme: &'a nsACString) -> bool { + if scheme.is_empty() { + return false; + } + + // first char must be alpha + if !scheme[0].is_ascii_alphabetic() { + return false; + } + + scheme[1..] + .iter() + .all(|a_char| is_valid_scheme_char(*a_char)) +} + +fn is_valid_scheme_char(a_char: u8) -> bool { + a_char.is_ascii_alphanumeric() || a_char == b'+' || a_char == b'.' || a_char == b'-' }