Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cpp/src/arrow/json/converter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,10 @@ TEST(ConverterTest, Decimal128And256PrecisionError) {
std::shared_ptr<StructArray> parse_array;
ASSERT_OK(ParseFromString(options, json_source, &parse_array));

// FromString now rejects precision overflow before the JSON converter wraps
// the error, so match on the shared body only.
std::string error_msg =
"Invalid: Failed to convert JSON to " + decimal_type->ToString() +
": 123456789012345678901234567890.0123456789 requires precision 40";
"123456789012345678901234567890.0123456789 requires precision 40";
EXPECT_RAISES_WITH_MESSAGE_THAT(
Invalid, ::testing::HasSubstr(error_msg),
Convert(decimal_type, parse_array->GetFieldByName("")));
Expand Down
33 changes: 26 additions & 7 deletions cpp/src/arrow/util/decimal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,9 @@ std::string Decimal128::ToString(int32_t scale) const {
// Iterates over input and for each group of kInt64DecimalDigits multiple out by
// the appropriate power of 10 necessary to add source parsed as uint64 and
// then adds the parsed value of source.
static inline void ShiftAndAdd(std::string_view input, uint64_t out[], size_t out_size) {
// Returns true if the result overflowed out_size words.
static inline bool ShiftAndAdd(std::string_view input, uint64_t out[], size_t out_size) {
bool overflow = false;
for (size_t posn = 0; posn < input.size();) {
const size_t group_size = std::min(kInt64DecimalDigits, input.size() - posn);
const uint64_t multiple = kUInt64PowersOfTen[group_size];
Expand All @@ -783,8 +785,10 @@ static inline void ShiftAndAdd(std::string_view input, uint64_t out[], size_t ou
out[i] = static_cast<uint64_t>(tmp & 0xFFFFFFFFFFFFFFFFULL);
chunk = static_cast<uint64_t>(tmp >> 64);
}
overflow |= chunk != 0;
posn += group_size;
}
return overflow;
}

namespace {
Expand Down Expand Up @@ -891,9 +895,23 @@ Status DecimalFromString(const char* type_name, std::string_view s, Decimal* out
if (out != nullptr) {
static_assert(Decimal::kBitWidth % 64 == 0, "decimal bit-width not a multiple of 64");
std::array<uint64_t, Decimal::kBitWidth / 64> little_endian_array{};
ShiftAndAdd(dec.whole_digits, little_endian_array.data(), little_endian_array.size());
ShiftAndAdd(dec.fractional_digits, little_endian_array.data(),
little_endian_array.size());
bool overflow = ShiftAndAdd(dec.whole_digits, little_endian_array.data(),
little_endian_array.size());
overflow |= ShiftAndAdd(dec.fractional_digits, little_endian_array.data(),
little_endian_array.size());
// The magnitude must fit in the signed range, i.e. be at most 2^(N-1) - 1,
// or exactly 2^(N-1) for a negative number.
constexpr uint64_t kSignBit = uint64_t{1} << 63;
if (!overflow && (little_endian_array.back() & kSignBit)) {
overflow = dec.sign != '-' || little_endian_array.back() != kSignBit ||
std::any_of(little_endian_array.begin(), little_endian_array.end() - 1,
[](uint64_t word) { return word != 0; });
}
if (overflow) {
return Status::Invalid(s, " requires precision ", parsed_precision,
" which exceeds the ", type_name, " maximum of ",
Decimal::kMaxPrecision);
}
*out = Decimal(bit_util::little_endian::ToNative(little_endian_array));
if (dec.sign == '-') {
out->Negate();
Expand Down Expand Up @@ -954,9 +972,10 @@ Status SimpleDecimalFromString(const char* type_name, std::string_view s,

if (out != nullptr) {
uint64_t value{0};
ShiftAndAdd(dec.whole_digits, &value, 1);
ShiftAndAdd(dec.fractional_digits, &value, 1);
if (value > static_cast<uint64_t>(
bool overflow = ShiftAndAdd(dec.whole_digits, &value, 1);
overflow |= ShiftAndAdd(dec.fractional_digits, &value, 1);
if (overflow ||
value > static_cast<uint64_t>(
std::numeric_limits<typename DecimalClass::ValueType>::max())) {
return Status::Invalid("The string '", s, "' cannot be represented as ", type_name);
}
Expand Down
34 changes: 28 additions & 6 deletions cpp/src/arrow/util/decimal_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ TEST(Decimal64Test, TestIntMinFitsPrecision) {
ASSERT_FALSE(d.FitsInPrecision(18));
}

TEST(Decimal64Test, FromStringOverflow) {
// 2^64 + 1 would wrap around to 1 in a uint64_t
ASSERT_RAISES(Invalid, Decimal64::FromString("18446744073709551617"));
ASSERT_RAISES(Invalid, Decimal32::FromString("18446744073709551617"));
}

TYPED_TEST_SUITE(DecimalFromStringTest, DecimalTypes);

TYPED_TEST(DecimalFromStringTest, Basics) { this->TestBasics(); }
Expand Down Expand Up @@ -435,15 +441,22 @@ TEST(Decimal128Test, FromStringLimits) {
ASSERT_RAISES(Invalid, Decimal128::FromString("-9e39"));
ASSERT_RAISES(Invalid, Decimal128::FromString("9.9e40"));
ASSERT_RAISES(Invalid, Decimal128::FromString("-9.9e40"));
// XXX conversion overflows are currently not detected
// Digits that do not fit in 128 bits
ASSERT_RAISES(Invalid,
Decimal128::FromString("999999999999999999999999999999999999999"));
ASSERT_RAISES(Invalid,
Decimal128::FromString("170141183460469231731687303715884105728"));
ASSERT_RAISES(Invalid,
Decimal128::FromString("-170141183460469231731687303715884105729"));
ASSERT_RAISES(Invalid, Decimal128::FromString(
"1.55555555555555555555555555555555555555555555555555"));
// XXX conversion overflows through the exponent are currently not detected
// ASSERT_RAISES(Invalid, Decimal128::FromString("99e38"));
// ASSERT_RAISES(Invalid, Decimal128::FromString("-99e38"));
// ASSERT_RAISES(Invalid,
// Decimal128::FromString("999999999999999999999999999999999999999e1"));
// ASSERT_RAISES(Invalid,
// Decimal128::FromString("-999999999999999999999999999999999999999e1"));
// ASSERT_RAISES(Invalid,
// Decimal128::FromString("999999999999999999999999999999999999999"));

// No exponent, many fractional digits
AssertDecimalFromString("9.9999999999999999999999999999999999999", dec38times9pos, 38,
Expand Down Expand Up @@ -541,15 +554,24 @@ TEST(Decimal256Test, FromStringLimits) {
ASSERT_RAISES(Invalid, Decimal256::FromString("9.9e78"));
ASSERT_RAISES(Invalid, Decimal256::FromString("-9.9e78"));

// XXX conversion overflows are currently not detected
// Digits that do not fit in 256 bits
ASSERT_RAISES(Invalid,
Decimal256::FromString("999999999999999999999999999999999999999"
"99999999999999999999999999999999999999"));
ASSERT_RAISES(Invalid,
Decimal256::FromString("578960446186580977117854925043439539266"
"34992332820282019728792003956564819968"));
ASSERT_RAISES(Invalid,
Decimal256::FromString("-578960446186580977117854925043439539266"
"34992332820282019728792003956564819969"));

// XXX conversion overflows through the exponent are currently not detected
// ASSERT_RAISES(Invalid, Decimal256::FromString("99e76"));
// ASSERT_RAISES(Invalid, Decimal256::FromString("-99e76"));
// ASSERT_RAISES(Invalid,
// Decimal256::FromString("9999999999999999999999999999999999999999999999999999999999999999999999999999e1"));
// ASSERT_RAISES(Invalid,
// Decimal256::FromString("-9999999999999999999999999999999999999999999999999999999999999999999999999999e1"));
// ASSERT_RAISES(Invalid,
// Decimal256::FromString("99999999999999999999999999999999999999999999999999999999999999999999999999999"));

// No exponent, many fractional digits
AssertDecimalFromString(
Expand Down
Loading