Skip to content

Commit 4c9888f

Browse files
committed
rgb: add missing alloc error checks
1 parent 1ee9c3a commit 4c9888f

1 file changed

Lines changed: 39 additions & 10 deletions

File tree

src/cgif_rgb.c

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,31 +126,39 @@ static int32_t col_hash_collision_count(const uint8_t* rgb, const uint8_t* hashT
126126
return h;
127127
}
128128

129+
/* free the color hash table*/
130+
static void free_col_hash_table(colHashTable* colhash){
131+
free(colhash->frequ);
132+
free(colhash->hashTable);
133+
free(colhash->indexUsed);
134+
free(colhash->pPalette);
135+
free(colhash->colIdx);
136+
free(colhash);
137+
}
138+
129139
/* initialize hash table storing colors and their frequency */
130140
static colHashTable* init_col_hash_table(uint32_t tableSize){
131141
colHashTable* colhash = malloc(sizeof(colHashTable));
142+
if(colhash == NULL) {
143+
return NULL;
144+
}
145+
memset(colhash, 0, sizeof(colHashTable));
132146
colhash->tableSize = getNextPrimePower2(tableSize); // increase table size to next prime number
133147
colhash->frequ = malloc(sizeof(uint32_t) * colhash->tableSize);
134148
colhash->hashTable = malloc(3 * colhash->tableSize);
135149
colhash->indexUsed = malloc(colhash->tableSize);
136150
colhash->pPalette = malloc(3 * colhash->tableSize);
137151
colhash->colIdx = malloc(sizeof(uint32_t)*colhash->tableSize);
152+
if(!colhash->frequ || !colhash->hashTable || !colhash->indexUsed || !colhash->pPalette || !colhash->colIdx) {
153+
free_col_hash_table(colhash);
154+
return NULL;
155+
}
138156
colhash->cnt = 0; // no colors initially
139157
memset(colhash->pPalette, 0, 3 * colhash->tableSize); // unused part of color table is uninitialized otheriwse
140158
memset(colhash->indexUsed, 0, colhash->tableSize); // initially no entry in hash-table is used
141159
return colhash;
142160
}
143161

144-
/* free the color hash table*/
145-
static void free_col_hash_table(colHashTable* colhash){
146-
free(colhash->frequ);
147-
free(colhash->hashTable);
148-
free(colhash->indexUsed);
149-
free(colhash->pPalette);
150-
free(colhash->colIdx);
151-
free(colhash);
152-
}
153-
154162
/* increase the size of the color hash table */
155163
static void resize_col_hash_table(colHashTable* colhash){
156164
uint32_t tableSizeNew;
@@ -446,6 +454,9 @@ static colHashTable* get_color_histogram(const uint8_t* pImageDataRGB, uint32_t
446454
uint32_t cntCollision; // count the number of collision
447455
uint32_t tableSize = 262147; // initial size of the hash table
448456
colHashTable* colhash = init_col_hash_table(tableSize); // initialize the hash table storing all the colors
457+
if(colhash == NULL) {
458+
return NULL;
459+
}
449460
*pHasAlpha = 0; // assume no alpha channel until it is found
450461
const uint8_t sizePixel = fmtChan; // number of bytes for one pixel (e.g. 3 for RGB, 4 for RGBa)
451462
for(uint32_t i = 0; i < numPixel; ++i) {
@@ -521,6 +532,9 @@ CGIFrgb* cgif_rgb_newgif(const CGIFrgb_Config* pConfig) {
521532
CGIFrgb* pGIFrgb;
522533

523534
pGIFrgb = malloc(sizeof(CGIFrgb));
535+
if(pGIFrgb == NULL) {
536+
return NULL;
537+
}
524538
memset(pGIFrgb, 0, sizeof(CGIFrgb));
525539
idxConfig.pWriteFn = pConfig->pWriteFn;
526540
idxConfig.pContext = pConfig->pContext;
@@ -558,16 +572,31 @@ cgif_result cgif_rgb_addframe(CGIFrgb* pGIF, const CGIFrgb_FrameConfig* pConfig)
558572
return CGIF_ERROR;
559573
}
560574
pNewBef = malloc(pConfig->fmtChan * MULU16(imageWidth, imageHeight));
575+
if(pNewBef == NULL) {
576+
pGIF->curResult = CGIF_EALLOC;
577+
return pGIF->curResult;
578+
}
561579
memcpy(pNewBef, pConfig->pImageData, pConfig->fmtChan * MULU16(imageWidth, imageHeight));
562580
fConfig.pLocalPalette = aPalette;
563581
fConfig.pImageData = malloc(pGIF->config.width * (uint32_t)pGIF->config.height);
582+
if(fConfig.pImageData == NULL) {
583+
free(pNewBef);
584+
pGIF->curResult = CGIF_EALLOC;
585+
return pGIF->curResult;
586+
}
564587
fConfig.delay = pConfig->delay;
565588
fConfig.attrFlags = CGIF_FRAME_ATTR_USE_LOCAL_TABLE;
566589
if(pConfig->attrFlags & CGIF_RGB_FRAME_ATTR_INTERLACED) {
567590
fConfig.attrFlags |= CGIF_FRAME_ATTR_INTERLACED;
568591
}
569592

570593
colHashTable* colhash = get_color_histogram(pConfig->pImageData, numPixel, pConfig->fmtChan, &hasAlpha);
594+
if(colhash == NULL) {
595+
free(pNewBef);
596+
free(fConfig.pImageData);
597+
pGIF->curResult = CGIF_EALLOC;
598+
return pGIF->curResult;
599+
}
571600
const uint8_t bDither = !(pConfig->attrFlags & CGIF_RGB_FRAME_ATTR_NO_DITHERING);
572601
const int sizeLCT = quantize_and_dither(colhash, pConfig->pImageData, numPixel, pGIF->config.width, pConfig->fmtChan, fConfig.pImageData, aPalette, 8, bDither, hasAlpha, pGIF->pBefImageData, pGIF->befFmtChan);
573602
free_col_hash_table(colhash);

0 commit comments

Comments
 (0)