Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential OOB access during huffman decompression #396

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/huffman.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ static int get_bit( byte *fin ) {

/* Get a symbol */

static void Huff_offsetReceive( node_t *node, int *ch, byte *fin, int *offset ) {
static void Huff_offsetReceive( node_t *node, int *ch, byte *fin, int readsize, int *offset ) {
bloc = *offset;
while ( node && node->symbol == INTERNAL_NODE ) {
while ( node && node->symbol == INTERNAL_NODE && bloc < readsize ) {

if ( get_bit( fin ) ) {
node = node->right;
Expand All @@ -61,6 +61,13 @@ static void Huff_offsetReceive( node_t *node, int *ch, byte *fin, int *offset )
node = node->left;

}

if ( bloc >= readsize ) {
//Com_PrintError("OOB buffer access\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's worth printing. Why comment it? 😄

*ch = 7; // EOF
*offset = bloc;
return;
}
}
if ( !node ) {
*ch = 0;
Expand Down Expand Up @@ -128,7 +135,7 @@ int MSG_ReadBitsCompress(const byte* input, int readsize, byte* outputBuf, int o
}

for(offset = 0, i = 0; offset < readsize && i < outputBufSize; i++){
Huff_offsetReceive( msgHuff.tree, &get, (byte*)input, &offset);
Huff_offsetReceive( msgHuff.tree, &get, (byte*)input, readsize, &offset);
*outptr = (byte)get;
outptr++;
}
Expand Down