Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix int128 cast to unsigned #2314

Merged
merged 2 commits into from
Oct 31, 2023
Merged
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
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