LibCrypto: Make constructing a BigInteger from string fallible

Previously, constructing a `UnsignedBigInteger::from_base()` could
produce an incorrect result if the input string contained a valid
Base36 digit that was out of range of the given base. The same method
would also crash if the input string contained an invalid Base36 digit.
An error is now returned in both these cases.

Constructing a BigFraction from string is now also fallible, so that we
can handle the case where we are given an input string with invalid
digits.
This commit is contained in:
Tim Ledbetter 2024-01-12 21:34:23 +00:00 committed by Andrew Kaster
commit 48a3a02238
Notes: sideshowbarker 2024-07-17 05:18:58 +09:00
11 changed files with 68 additions and 57 deletions

View file

@ -63,7 +63,7 @@ public:
size_t export_data(Bytes, bool remove_leading_zeros = false) const;
[[nodiscard]] static UnsignedBigInteger from_base(u16 N, StringView str);
[[nodiscard]] static ErrorOr<UnsignedBigInteger> from_base(u16 N, StringView str);
[[nodiscard]] ErrorOr<String> to_base(u16 N) const;
[[nodiscard]] ByteString to_base_deprecated(u16 N) const;
@ -161,5 +161,5 @@ struct AK::Formatter<Crypto::UnsignedBigInteger> : Formatter<StringView> {
inline Crypto::UnsignedBigInteger
operator""_bigint(char const* string, size_t length)
{
return Crypto::UnsignedBigInteger::from_base(10, { string, length });
return MUST(Crypto::UnsignedBigInteger::from_base(10, { string, length }));
}