Skip to content

Commit 162b23a

Browse files
author
aleruggeri87
committed
Cap bufsiz variable (long) to avoid overflow in Alphabit & Rabbit
With nb > 2^(31+5), bufsiz is not correctly computed (can lead to a negative value); furthermore, with nb > 2^(31-2), util_Min() in ufile_CreateReadBin() could miscalculate the minimum value and the following util_Calloc() returns an error. To avoid all of this, bufsiz has been limited to LONG_MAX/4 (in any case, the maximum allocated buffer will be ARRAYDIM << LONG_MAX/4).
1 parent 9f83fe2 commit 162b23a

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

testu01/bbattery.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2751,7 +2751,11 @@ static void Alphabit (unif01_Gen * gen, char *fname, double nb, int r, int s,
27512751
util_Assert (nb > 0, "Alphabit: nb <= 0");
27522752
/* Bits will be read as 32-bit unsigned integers */
27532753
nb -= fmod (nb, 32.0);
2754-
bufsiz = nb / 32.0;
2754+
if(nb < LONG_MAX / 4) {
2755+
bufsiz = nb / 32.0;
2756+
} else {
2757+
bufsiz = LONG_MAX / 4;
2758+
}
27552759

27562760
if (blocFlag) {
27572761
gen0 = ufile_CreateReadBin (fname, bufsiz);
@@ -3300,7 +3304,11 @@ static void Rabbit (unif01_Gen * gen, char *fname, double nb, int Rep[])
33003304
/* Bits will be read as 32-bit unsigned integers */
33013305
nb -= fmod (nb, 32.0);
33023306
nw = nb / 32.0;
3303-
bufsiz = nw;
3307+
if(nw < LONG_MAX/4) {
3308+
bufsiz = nw;
3309+
} else {
3310+
bufsiz = LONG_MAX/4;
3311+
}
33043312

33053313
if (NULL == gen) {
33063314
gen = ufile_CreateReadBin (fname, bufsiz);

0 commit comments

Comments
 (0)