From 4f3d1e60ca85c4e1681324b049a0637d2e5a560b Mon Sep 17 00:00:00 2001 From: Ashleyhx Date: Tue, 31 Oct 2023 14:09:08 -0400 Subject: [PATCH 1/2] fix int128 cast to unsigned --- src/common/types/int128_t.cpp | 14 +++++++++----- test/test_files/tinysnb/cast/cast_error.test | 12 ++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/common/types/int128_t.cpp b/src/common/types/int128_t.cpp index bb8cedc1eb..ae1eb4929b 100644 --- a/src/common/types/int128_t.cpp +++ b/src/common/types/int128_t.cpp @@ -346,7 +346,7 @@ int128_t Int128_t::Mod(int128_t lhs, int128_t rhs) { //=============================================================================================== // Cast operation //=============================================================================================== -template +template bool TryCastInt128Template(int128_t input, DST& result) { switch (input.high) { case 0: @@ -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::max()) - uint64_t(std::numeric_limits::max())) { result = static_cast(std::numeric_limits::max() - input.low) - 1; @@ -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(input, result); + return TryCastInt128Template(input, result); } template<> bool Int128_t::tryCast(int128_t input, uint16_t& result) { - return TryCastInt128Template(input, result); + return TryCastInt128Template(input, result); } template<> bool Int128_t::tryCast(int128_t input, uint32_t& result) { - return TryCastInt128Template(input, result); + return TryCastInt128Template(input, result); } template<> bool Int128_t::tryCast(int128_t input, uint64_t& result) { - return TryCastInt128Template(input, result); + return TryCastInt128Template(input, result); } template<> diff --git a/test/test_files/tinysnb/cast/cast_error.test b/test/test_files/tinysnb/cast/cast_error.test index 2c63366645..21b1513630 100644 --- a/test/test_files/tinysnb/cast/cast_error.test +++ b/test/test_files/tinysnb/cast/cast_error.test @@ -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. From 5fae7eb81e6cb4e2f44590fb174623928168649a Mon Sep 17 00:00:00 2001 From: Ashleyhx Date: Tue, 31 Oct 2023 14:20:46 -0400 Subject: [PATCH 2/2] format --- src/common/types/int128_t.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/types/int128_t.cpp b/src/common/types/int128_t.cpp index ae1eb4929b..05d8c0af39 100644 --- a/src/common/types/int128_t.cpp +++ b/src/common/types/int128_t.cpp @@ -346,7 +346,7 @@ int128_t Int128_t::Mod(int128_t lhs, int128_t rhs) { //=============================================================================================== // Cast operation //=============================================================================================== -template +template bool TryCastInt128Template(int128_t input, DST& result) { switch (input.high) { case 0: