|
1 | 1 | #include <stdlib.h> |
2 | 2 | #include <string.h> |
| 3 | +#include <stdint.h> |
3 | 4 |
|
4 | 5 | #include "cgif_raw.h" |
5 | 6 |
|
@@ -330,7 +331,15 @@ static int LZW_GenerateStream(LZWResult* pResult, const uint32_t numPixel, const |
330 | 331 | // where N = max dictionary resets = numPixel / (MAX_DICT_LEN - initDictLen - 2) |
331 | 332 | entriesPerCycle = MAX_DICT_LEN - initDictLen - 2; // maximum added number of dictionary entries per cycle: -2 to account for start and end code |
332 | 333 | maxResets = numPixel / entriesPerCycle; |
333 | | - pContext->pLZWData = malloc(sizeof(uint16_t) * ((size_t)numPixel + 2 + maxResets)); |
| 334 | + |
| 335 | + // check for integer overflow in dictArraySize calculation and malloc |
| 336 | + if (numPixel > (SIZE_MAX - 2 - maxResets) || ((size_t)numPixel + 2 + maxResets) > (SIZE_MAX / sizeof(uint16_t))) { |
| 337 | + r = CGIF_EALLOC; |
| 338 | + goto LZWGENERATE_Cleanup; |
| 339 | + } |
| 340 | + const size_t dictArraySize = (size_t)numPixel + 2 + maxResets; |
| 341 | + pContext->pLZWData = malloc(sizeof(uint16_t) * dictArraySize); |
| 342 | + |
334 | 343 | if(pContext->pLZWData == NULL) { |
335 | 344 | r = CGIF_EALLOC; |
336 | 345 | goto LZWGENERATE_Cleanup; |
@@ -454,6 +463,12 @@ CGIFRaw* cgif_raw_newgif(const CGIFRaw_Config* pConfig) { |
454 | 463 | uint8_t aHeader[SIZE_MAIN_HEADER]; |
455 | 464 | CGIFRaw* pGIF; |
456 | 465 | int rWrite; |
| 466 | + |
| 467 | + // width or height cannot be zero |
| 468 | + if(!pConfig->width || !pConfig->height) { |
| 469 | + return NULL; |
| 470 | + } |
| 471 | + |
457 | 472 | // check for invalid GCT size |
458 | 473 | if(pConfig->sizeGCT > 256) { |
459 | 474 | return NULL; // invalid GCT size |
@@ -503,6 +518,12 @@ cgif_result cgif_raw_addframe(CGIFRaw* pGIF, const CGIFRaw_FrameConfig* pConfig) |
503 | 518 | LZWResult encResult; |
504 | 519 | int r, rWrite; |
505 | 520 | const int useLCT = pConfig->sizeLCT; // LCT stands for "local color table" |
| 521 | + |
| 522 | + // width or height cannot be zero |
| 523 | + if(!pConfig->width || !pConfig->height) { |
| 524 | + pGIF->curResult = CGIF_ERROR; |
| 525 | + return pGIF->curResult; |
| 526 | + } |
506 | 527 | const int isInterlaced = (pConfig->attrFlags & CGIF_RAW_FRAME_ATTR_INTERLACED) ? 1 : 0; |
507 | 528 | uint16_t numEffColors; // number of effective colors |
508 | 529 | uint16_t initDictLen; |
|
0 commit comments