Merge pull request #1545 from samael500/master

Fix integer overflow
This commit is contained in:
alexey-milovidov 2017-11-25 01:43:48 +03:00 committed by GitHub
commit 3a0e0173ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 3 deletions

View File

@ -210,9 +210,9 @@ struct ModuloImpl
template <typename Result = ResultType>
static inline Result apply(A a, B b)
{
throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<A>::Type(b));
throwIfDivisionLeadsToFPE(typename NumberTraits::ToInteger<A>::Type(a), typename NumberTraits::ToInteger<B>::Type(b));
return typename NumberTraits::ToInteger<A>::Type(a)
% typename NumberTraits::ToInteger<A>::Type(b);
% typename NumberTraits::ToInteger<B>::Type(b);
}
};
@ -434,7 +434,7 @@ struct AbsImpl
template <typename A, typename B>
struct GCDImpl
{
using ResultType = typename NumberTraits::ResultOfIntegerDivision<A, B>::Type;
using ResultType = typename NumberTraits::ResultOfAdditionMultiplication<A, B>::Type;
template <typename Result = ResultType>
static inline Result apply(A a, B b)

View File

@ -8,6 +8,9 @@
1
2147483647
1
5
255
1
5120
121
256

View File

@ -9,6 +9,9 @@ select gcd(255, 254);
select gcd(2147483647, 2147483646);
select gcd(4611686011984936962, 2147483647);
select gcd(-2147483648, 1);
select gcd(255, 515);
select gcd(255, 510);
select gcd(255, 512);
-- test lcm
select lcm(1280, 1024);
select lcm(11, 121);

View File

@ -0,0 +1,13 @@
8
1
255
255
255
0
127
0
-1
57
3
20
16

View File

@ -0,0 +1,13 @@
SELECT 1000 % 32;
SELECT 7 % 3;
SELECT 255 % 510;
SELECT 255 % 512;
SELECT 255 % 1000000009;
SELECT 0 % 255;
SELECT 2147483647 % 255;
SELECT -1 % -1;
SELECT -1 % -2;
SELECT 255 % 99;
SELECT 42 % 13;
SELECT 42 % 22;
SELECT 1234567 % 123;