This forum has been archived. All content is frozen. Please use KDE Discuss instead.

integer overflow fix

Tags: None
(comma "," separated)
tuinenga
Registered Member
Posts
2
Karma
0
OS

integer overflow fix

Tue Jun 14, 2011 4:49 pm
In Eigen 3.0.1 .../Core/util/XprHelper.h, line 131, is:
Code: Select all
((!is_dynamic_size_storage) && (((MaxCols*MaxRows) % packet_traits<Scalar>::size) == 0))

My suggested fix is to remainder the multiplier and multiplicand, first, as the product of an integer multiple and any integer is also an integer multiple (by definition) and won't contribute to the final remainder. This reduces the range of the new product to less than the square of the modulus.
expand:
(A*B)%C = {[(A/C)*C + A%C]*[(B/C)*C + B%C]}%C
cross multiply:
(A*B)%C = {[(A/C)*C]*[(B/C)*C] + (A/C)*C*(B%C) + (B/C)*C*(A%C) + (A%C)*(B%C)}%C
distribute:
(A*B)%C = {[(A/C)*C]*[(B/C)*C]}%C + [(A/C)*C*(B%C)]%C + [(B/C)*C*(A%C)]%C + [(A%C)*(B%C)]%C
cancel:
(A*B)%C = [(A%C)*(B%C)]%C
Code: Select all
((!is_dynamic_size_storage) && ((((MaxCols % packet_traits<Scalar>::size)*(MaxRows % packet_traits<Scalar>::size)) % packet_traits<Scalar>::size) == 0))
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: integer overflow fix

Tue Jun 14, 2011 6:58 pm
ok, fair enough, but those values are supposed to be very small since they target stack allocation. For instance it really makes no sense to use them values greater than let's say 32...
tuinenga
Registered Member
Posts
2
Karma
0
OS

Re: integer overflow fix

Tue Jun 14, 2011 10:28 pm
Stumbled on it; overflows of intermediate values are a pet peeve.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: integer overflow fix

Wed Jun 15, 2011 6:04 am
which values did you use? because if you specify them, then somewhere in the code we'll have:

Scalar data[MaxCols*MaxRows];

if MaxCols*MaxRows overflows that means you are trying to allocate more than 2^31 * sizeof(Scalar) bytes on the stack!

so I still don't really see how your fix can really improve things.

Maybe your compiler did not used "int" but "short" for the enum values???


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], q.ignora, watchstar