Make BigNum::from_decimal safe.

GitOrigin-RevId: bc414c62024d0f971d3400fec738545f898654a4
This commit is contained in:
levlam 2018-08-09 21:03:21 +03:00
parent c44361fb60
commit 6c45d2c479
4 changed files with 24 additions and 6 deletions

View File

@ -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))); 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; BigNum result;
int err = BN_dec2bn(&result.impl_->big_num, str.c_str()); int res = BN_dec2bn(&result.impl_->big_num, str.c_str());
LOG_IF(FATAL, err == 0); if (res == 0 || static_cast<size_t>(res) != str.size()) {
return Status::Error(PSLICE() << "Failed to parse \"" << str << "\" as BigNum");
}
return result; return result;
} }

View File

@ -11,6 +11,7 @@
#if TD_HAVE_OPENSSL #if TD_HAVE_OPENSSL
#include "td/utils/Slice.h" #include "td/utils/Slice.h"
#include "td/utils/Status.h"
namespace td { namespace td {
@ -41,7 +42,7 @@ class BigNum {
static BigNum from_binary(Slice str); 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); static BigNum from_raw(void *openssl_big_num);

View File

@ -5,6 +5,7 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// //
#include "td/utils/base64.h" #include "td/utils/base64.h"
#include "td/utils/BigNum.h"
#include "td/utils/HttpUrl.h" #include "td/utils/HttpUrl.h"
#include "td/utils/logging.h" #include "td/utils/logging.h"
#include "td/utils/misc.h" #include "td/utils/misc.h"
@ -404,3 +405,17 @@ TEST(Misc, unicode) {
test_unicode(unicode_to_lower); test_unicode(unicode_to_lower);
test_unicode(remove_diacritics); 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());
}

View File

@ -70,8 +70,8 @@ static std::vector<PqQuery> gen_pq_queries() {
} }
static void test_pq(uint64 first, uint64 second) { static void test_pq(uint64 first, uint64 second) {
BigNum p = BigNum::from_decimal(PSLICE() << first); BigNum p = BigNum::from_decimal(PSLICE() << first).move_as_ok();
BigNum q = BigNum::from_decimal(PSLICE() << second); BigNum q = BigNum::from_decimal(PSLICE() << second).move_as_ok();
BigNum pq; BigNum pq;
BigNumContext context; BigNumContext context;