Skip to content

Commit 884099c

Browse files
committed
Defensive code
1 parent d931c4f commit 884099c

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

src/entropy/EntropyUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ int EntropyUtils::normalizeFrequencies(uint freqs[], uint alphabet[], int length
253253
break;
254254
}
255255

256-
freqs[idxMax] -= delta;
256+
freqs[idxMax] = max(freqs[idxMax] - delta, uint(1));
257257
return alphabetSize;
258258
}
259259

src/entropy/HuffmanEncoder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ int HuffmanEncoder::limitCodeLengths(const uint alphabet[], uint freqs[], uint16
197197
}
198198

199199
// Renormalize to a smaller scale
200-
EntropyUtils::normalizeFrequencies(f, alpha, count, totalFreq, HuffmanCommon::MAX_CHUNK_SIZE >> 3);
200+
int alphabetSize = EntropyUtils::normalizeFrequencies(f, alpha, count, totalFreq, HuffmanCommon::MAX_CHUNK_SIZE >> 3);
201201

202-
for (int i = 0; i < count; i++) {
202+
for (int i = 0; i < alphabetSize; i++) {
203203
freqs[alphabet[i]] = f[i];
204204
ranks[i] = (f[i] << 8) | alphabet[i];
205205
}

0 commit comments

Comments
 (0)