Skip to content

Commit

Permalink
fix int128 cast to unsigned (#2314)
Browse files Browse the repository at this point in the history
* fix int128 cast to unsigned
  • Loading branch information
Ashleyhx committed Oct 31, 2023
1 parent e7b0c2f commit 4a9fe94
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/common/types/int128_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ bool TryCastInt128Template(int128_t input, DST& result) {
}
break;
case -1:
if constexpr (!SIGNED) {
throw common::OverflowException(
"Cast failed. Cannot cast " + Int128_t::ToString(input) + " to unsigned type.");
}
if (input.low >=
uint64_t(std::numeric_limits<DST>::max()) - uint64_t(std::numeric_limits<DST>::max())) {
result = static_cast<DST>(std::numeric_limits<DST>::max() - input.low) - 1;
Expand Down Expand Up @@ -391,22 +395,22 @@ bool Int128_t::tryCast(int128_t input, int64_t& result) {

template<>
bool Int128_t::tryCast(int128_t input, uint8_t& result) {
return TryCastInt128Template<uint8_t>(input, result);
return TryCastInt128Template<uint8_t, false>(input, result);
}

template<>
bool Int128_t::tryCast(int128_t input, uint16_t& result) {
return TryCastInt128Template<uint16_t>(input, result);
return TryCastInt128Template<uint16_t, false>(input, result);
}

template<>
bool Int128_t::tryCast(int128_t input, uint32_t& result) {
return TryCastInt128Template<uint32_t>(input, result);
return TryCastInt128Template<uint32_t, false>(input, result);
}

template<>
bool Int128_t::tryCast(int128_t input, uint64_t& result) {
return TryCastInt128Template<uint64_t>(input, result);
return TryCastInt128Template<uint64_t, false>(input, result);
}

template<>
Expand Down
12 changes: 12 additions & 0 deletions test/test_files/tinysnb/cast/cast_error.test
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,18 @@ Conversion exception: Cast failed. -1 is not in UINT8 range.
-STATEMENT RETURN TO_INT128(170141183460469231731687303715884105728);
---- error
Conversion exception: Cast failed. 170141183460469231731687303715884105728 is not within INT128 range.
-STATEMENT RETURN TO_INT128("-170141183460469231731687303715884105728");
---- error
Conversion exception: Cast failed. -170141183460469231731687303715884105728 is not within INT128 range.
-STATEMENT RETURN TO_UINT8(TO_INT128(-1));
---- error
Overflow exception: Cast failed. Cannot cast -1 to unsigned type.
-STATEMENT RETURN TO_UINT16(TO_INT128(-10));
---- error
Overflow exception: Cast failed. Cannot cast -10 to unsigned type.
-STATEMENT RETURN TO_UINT64(TO_INT128(-15));
---- error
Overflow exception: Cast failed. Cannot cast -15 to unsigned type.
-STATEMENT RETURN TO_INT128(170141183460469231731687303715884105727) + TO_INT128(10);
---- error
Overflow exception: INT128 is out of range: cannot add.
Expand Down

0 comments on commit 4a9fe94

Please sign in to comment.