diff --git a/widget/windows/GfxInfo.cpp b/widget/windows/GfxInfo.cpp index 0e1b4002d387..6d8429f1cc54 100644 --- a/widget/windows/GfxInfo.cpp +++ b/widget/windows/GfxInfo.cpp @@ -310,7 +310,7 @@ uint32_t ParseIDFromDeviceID(const nsAString& key, const nsAString& prefix, return 0x5143; } nsresult err; - return id.ToInteger(&err, 16); + return id.ToUnsignedInteger(&err, 16); } // OS version in 16.16 major/minor form diff --git a/xpcom/string/nsTSubstring.cpp b/xpcom/string/nsTSubstring.cpp index dfe11969b7a2..412465188ad6 100644 --- a/xpcom/string/nsTSubstring.cpp +++ b/xpcom/string/nsTSubstring.cpp @@ -1317,6 +1317,9 @@ int_type ToIntegerCommon(const nsTSubstring& aSrc, nsresult* aErrorCode, break; // clang-format on case '-': + if constexpr (!std::is_signed_v) { + return 0; + } negate = true; break; default: @@ -1384,6 +1387,12 @@ int32_t nsTSubstring::ToInteger(nsresult* aErrorCode, return ToIntegerCommon(*this, aErrorCode, aRadix); } +template +uint32_t nsTSubstring::ToUnsignedInteger(nsresult* aErrorCode, + uint32_t aRadix) const { + return ToIntegerCommon(*this, aErrorCode, aRadix); +} + /** * nsTSubstring::ToInteger64 */ diff --git a/xpcom/string/nsTSubstring.h b/xpcom/string/nsTSubstring.h index 0b4022823f4f..622b931afb42 100644 --- a/xpcom/string/nsTSubstring.h +++ b/xpcom/string/nsTSubstring.h @@ -382,6 +382,14 @@ class nsTSubstring : public mozilla::detail::nsTStringRepr { */ int32_t ToInteger(nsresult* aErrorCode, uint32_t aRadix = 10) const; + /** + * Perform string to uint conversion. + * @param aErrorCode will contain error if one occurs + * @param aRadix is the radix to use. Only 10 and 16 are supported. + * @return int rep of string value, and possible (out) error code + */ + uint32_t ToUnsignedInteger(nsresult* aErrorCode, uint32_t aRadix = 10) const; + /** * Perform string to 64-bit int conversion. * @param aErrorCode will contain error if one occurs diff --git a/xpcom/tests/gtest/TestStrings.cpp b/xpcom/tests/gtest/TestStrings.cpp index e9649f2a5cde..a2bf57db9013 100644 --- a/xpcom/tests/gtest/TestStrings.cpp +++ b/xpcom/tests/gtest/TestStrings.cpp @@ -1286,6 +1286,32 @@ TEST_F(Strings, string_tointeger) { } } +struct ToUnsignedIntegerTest { + const char* str; + uint32_t radix; + uint32_t result; + nsresult rv; +}; + +static const ToUnsignedIntegerTest kToUnsignedIntegerTests[] = { + {"123", 10, 123, NS_OK}, + {"7b", 16, 123, NS_OK}, + {"90194313659", 10, 0, NS_ERROR_ILLEGAL_VALUE}, + {"ffffffff", 16, 0xffffffff, NS_OK}, + {"4294967295", 10, 4294967295, NS_OK}, + {"8abc1234", 16, 0x8abc1234, NS_OK}, + {"-194313659", 10, 0, NS_ERROR_ILLEGAL_VALUE}, + {nullptr, 0, 0, NS_OK}}; + +TEST_F(Strings, string_to_unsigned_integer) { + nsresult rv; + for (const ToUnsignedIntegerTest* t = kToUnsignedIntegerTests; t->str; ++t) { + uint32_t result = nsAutoCString(t->str).ToUnsignedInteger(&rv, t->radix); + EXPECT_EQ(rv, t->rv); + EXPECT_EQ(result, t->result); + } +} + static void test_parse_string_helper(const char* str, char separator, int len, const char* s1, const char* s2) { nsCString data(str);