Skip to content

Commit

Permalink
Add range checks to fix a few crash issues in stb_image issues 1289 a…
Browse files Browse the repository at this point in the history
…nd 1291
  • Loading branch information
NBickford-NV committed Nov 29, 2022
1 parent 84b9401 commit 96fe76c
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions stb_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -1985,9 +1985,12 @@ static int stbi__build_huffman(stbi__huffman *h, int *count)
int i,j,k=0;
unsigned int code;
// build size list for each symbol (from JPEG spec)
for (i=0; i < 16; ++i)
for (j=0; j < count[i]; ++j)
for (i=0; i < 16; ++i) {
for (j=0; j < count[i]; ++j) {
h->size[k++] = (stbi_uc) (i+1);
if(k >= 257) return stbi__err("bad size list","Corrupt JPEG");
}
}
h->size[k] = 0;

// compute actual symbols (from jpeg spec)
Expand Down Expand Up @@ -2112,6 +2115,8 @@ stbi_inline static int stbi__jpeg_huff_decode(stbi__jpeg *j, stbi__huffman *h)

// convert the huffman code to the symbol id
c = ((j->code_buffer >> (32 - k)) & stbi__bmask[k]) + h->delta[k];
if(c < 0 || c >= 256) // symbol id out of bounds!
return -1;
STBI_ASSERT((((j->code_buffer) >> (32 - h->size[c])) & stbi__bmask[h->size[c]]) == h->code[c]);

// convert the id to a symbol
Expand Down Expand Up @@ -3103,6 +3108,7 @@ static int stbi__process_marker(stbi__jpeg *z, int m)
sizes[i] = stbi__get8(z->s);
n += sizes[i];
}
if(n > 256) return stbi__err("bad DHT header","Corrupt JPEG"); // Loop over i < n would write past end of values!
L -= 17;
if (tc == 0) {
if (!stbi__build_huffman(z->huff_dc+th, sizes)) return 0;
Expand Down

0 comments on commit 96fe76c

Please sign in to comment.