Skip to content

Commit 56b4096

Browse files
jkbonfielddaviesrob
authored andcommitted
Remove divide by zero in rANS 4x8.
When given 0 bytes of input, we get a floating point exception in the code attempting to renormalise the frequencies. The test harness can't trigger this case as 0 is EOF in the read loop, but that has been changed so a null file still calls the compression. Also tested the other entropy encoders, but they already coped fine. Bug reported by Shubham Chandak
1 parent 1b1c5f1 commit 56b4096

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

htscodecs/rANS_static.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ unsigned char *rans_compress_O0(unsigned char *in, unsigned int in_size,
9696
free(out_buf);
9797
return NULL;
9898
}
99-
tr = ((uint64_t)TOTFREQ<<31)/in_size + (1<<30)/in_size;
99+
tr = in_size ? ((uint64_t)TOTFREQ<<31)/in_size + (1<<30)/in_size : 0;
100100

101101
normalise_harder:
102102
// Normalise so T[i] == TOTFREQ

tests/arith_dynamic_test.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,13 @@ int main(int argc, char **argv) {
266266
bytes += out_size;
267267
}
268268
} else {
269-
for (;;) {
269+
int loop = 0;
270+
for (;;loop++) {
270271
uint32_t in_size, out_size;
271272
unsigned char *out;
272273

273274
in_size = fread(in_buf, 1, BLK_SIZE, infp);
274-
if (in_size <= 0)
275+
if (loop && in_size <= 0)
275276
break;
276277

277278
if (in_size < 4)

tests/rANS_static4x16pr_test.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,13 @@ int main(int argc, char **argv) {
309309
bytes += out_size;
310310
}
311311
} else {
312-
for (;;) {
312+
int loop = 0;
313+
for (;;loop++) {
313314
uint32_t in_size, out_size;
314315
unsigned char *out;
315316

316317
in_size = fread(in_buf, 1, blk_size, infp);
317-
if (in_size <= 0)
318+
if (loop && in_size <= 0)
318319
break;
319320

320321
if (in_size < 4)

tests/rANS_static_test.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,13 @@ int main(int argc, char **argv) {
259259
bytes += out_size;
260260
}
261261
} else {
262-
for (;;) {
262+
int loop=0;
263+
for (;;loop++) {
263264
uint32_t in_size, out_size;
264265
unsigned char *out;
265266

266267
in_size = fread(in_buf, 1, BLK_SIZE, infp);
267-
if (in_size <= 0)
268+
if (loop && in_size <= 0)
268269
break;
269270

270271
out = rans_compress(in_buf, in_size, &out_size,

0 commit comments

Comments
 (0)