Make BigNum::from_decimal safe.
GitOrigin-RevId: bc414c62024d0f971d3400fec738545f898654a4
This commit is contained in:
parent
c44361fb60
commit
6c45d2c479
@ -85,10 +85,12 @@ BigNum BigNum::from_binary(Slice str) {
|
||||
return BigNum(make_unique<Impl>(BN_bin2bn(str.ubegin(), narrow_cast<int>(str.size()), nullptr)));
|
||||
}
|
||||
|
||||
BigNum BigNum::from_decimal(CSlice str) {
|
||||
Result<BigNum> BigNum::from_decimal(CSlice str) {
|
||||
BigNum result;
|
||||
int err = BN_dec2bn(&result.impl_->big_num, str.c_str());
|
||||
LOG_IF(FATAL, err == 0);
|
||||
int res = BN_dec2bn(&result.impl_->big_num, str.c_str());
|
||||
if (res == 0 || static_cast<size_t>(res) != str.size()) {
|
||||
return Status::Error(PSLICE() << "Failed to parse \"" << str << "\" as BigNum");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,7 @@
|
||||
#if TD_HAVE_OPENSSL
|
||||
|
||||
#include "td/utils/Slice.h"
|
||||
#include "td/utils/Status.h"
|
||||
|
||||
namespace td {
|
||||
|
||||
@ -41,7 +42,7 @@ class BigNum {
|
||||
|
||||
static BigNum from_binary(Slice str);
|
||||
|
||||
static BigNum from_decimal(CSlice str);
|
||||
static Result<BigNum> from_decimal(CSlice str);
|
||||
|
||||
static BigNum from_raw(void *openssl_big_num);
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
#include "td/utils/base64.h"
|
||||
#include "td/utils/BigNum.h"
|
||||
#include "td/utils/HttpUrl.h"
|
||||
#include "td/utils/logging.h"
|
||||
#include "td/utils/misc.h"
|
||||
@ -404,3 +405,17 @@ TEST(Misc, unicode) {
|
||||
test_unicode(unicode_to_lower);
|
||||
test_unicode(remove_diacritics);
|
||||
}
|
||||
|
||||
TEST(BigNum, from_decimal) {
|
||||
ASSERT_TRUE(BigNum::from_decimal("").is_error());
|
||||
ASSERT_TRUE(BigNum::from_decimal("a").is_error());
|
||||
ASSERT_TRUE(BigNum::from_decimal("123a").is_error());
|
||||
ASSERT_TRUE(BigNum::from_decimal("-123a").is_error());
|
||||
// ASSERT_TRUE(BigNum::from_decimal("-").is_error());
|
||||
ASSERT_TRUE(BigNum::from_decimal("123").is_ok());
|
||||
ASSERT_TRUE(BigNum::from_decimal("-123").is_ok());
|
||||
ASSERT_TRUE(BigNum::from_decimal("0").is_ok());
|
||||
ASSERT_TRUE(BigNum::from_decimal("-0").is_ok());
|
||||
ASSERT_TRUE(BigNum::from_decimal("-999999999999999999999999999999999999999999999999").is_ok());
|
||||
ASSERT_TRUE(BigNum::from_decimal("999999999999999999999999999999999999999999999999").is_ok());
|
||||
}
|
||||
|
@ -70,8 +70,8 @@ static std::vector<PqQuery> gen_pq_queries() {
|
||||
}
|
||||
|
||||
static void test_pq(uint64 first, uint64 second) {
|
||||
BigNum p = BigNum::from_decimal(PSLICE() << first);
|
||||
BigNum q = BigNum::from_decimal(PSLICE() << second);
|
||||
BigNum p = BigNum::from_decimal(PSLICE() << first).move_as_ok();
|
||||
BigNum q = BigNum::from_decimal(PSLICE() << second).move_as_ok();
|
||||
|
||||
BigNum pq;
|
||||
BigNumContext context;
|
||||
|
Loading…
Reference in New Issue
Block a user