Bug 1894077 - Fix subsystem id parsing. r=xpcom-reviewers,emilio

Previously if the we got a subsystem id like 87731043 we would
fail to parse it because it's too big to fit in int32_t.

This adds a ToUnsignedInteger helper that lets us parse
the large subsystem ids.

Differential Revision: https://phabricator.services.mozilla.com/D208931
This commit is contained in:
Jeff Muizelaar 2024-04-30 16:29:55 +00:00
parent fd1da0fb91
commit e572f8b87f
4 changed files with 44 additions and 1 deletions

View file

@ -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

View file

@ -1317,6 +1317,9 @@ int_type ToIntegerCommon(const nsTSubstring<T>& aSrc, nsresult* aErrorCode,
break;
// clang-format on
case '-':
if constexpr (!std::is_signed_v<int_type>) {
return 0;
}
negate = true;
break;
default:
@ -1384,6 +1387,12 @@ int32_t nsTSubstring<T>::ToInteger(nsresult* aErrorCode,
return ToIntegerCommon<T, int32_t>(*this, aErrorCode, aRadix);
}
template <typename T>
uint32_t nsTSubstring<T>::ToUnsignedInteger(nsresult* aErrorCode,
uint32_t aRadix) const {
return ToIntegerCommon<T, uint32_t>(*this, aErrorCode, aRadix);
}
/**
* nsTSubstring::ToInteger64
*/

View file

@ -382,6 +382,14 @@ class nsTSubstring : public mozilla::detail::nsTStringRepr<T> {
*/
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

View file

@ -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);