@@ -102,6 +102,13 @@ CGIF* cgif_newgif(CGIF_Config* pConfig) {
102102 // make a deep copy of global color tabele (GCT), if required.
103103 if ((pConfig -> attrFlags & CGIF_ATTR_NO_GLOBAL_TABLE ) == 0 ) {
104104 pGIF -> config .pGlobalPalette = malloc (pConfig -> numGlobalPaletteEntries * 3 );
105+ if (pGIF -> config .pGlobalPalette == NULL ) {
106+ if (pFile ) {
107+ fclose (pFile );
108+ }
109+ free (pGIF );
110+ return NULL ;
111+ }
105112 memcpy (pGIF -> config .pGlobalPalette , pConfig -> pGlobalPalette , pConfig -> numGlobalPaletteEntries * 3 );
106113 }
107114
@@ -333,7 +340,10 @@ static uint8_t* doWidthHeightOptim(CGIF* pGIF, CGIF_FrameConfig* pCur, CGIF_Fram
333340 }
334341
335342 // create new image data
336- pNewImageData = malloc (MULU16 (pResult -> width , pResult -> height )); // TBD check return value of malloc
343+ pNewImageData = malloc (MULU16 (pResult -> width , pResult -> height ));
344+ if (pNewImageData == NULL ) {
345+ return NULL ; // allocation failed
346+ }
337347 for (i = 0 ; i < pResult -> height ; ++ i ) {
338348 memcpy (pNewImageData + MULU16 (i , pResult -> width ), pCurImageData + MULU16 ((i + pResult -> top ), width ) + pResult -> left , pResult -> width );
339349 }
@@ -381,6 +391,9 @@ static cgif_result flushFrame(CGIF* pGIF, CGIF_Frame* pCur, CGIF_Frame* pBef) {
381391 // purge overlap of current frame and frame before (width - height optim), if required (CGIF_FRAME_GEN_USE_DIFF_WINDOW set)
382392 if (pCur -> config .genFlags & CGIF_FRAME_GEN_USE_DIFF_WINDOW ) {
383393 pTmpImageData = doWidthHeightOptim (pGIF , & pCur -> config , & pBef -> config , & dimResult );
394+ if (pTmpImageData == NULL ) {
395+ return CGIF_EALLOC ; // allocation failed in doWidthHeightOptim
396+ }
384397 width = dimResult .width ;
385398 height = dimResult .height ;
386399 top = dimResult .top ;
@@ -403,7 +416,10 @@ static cgif_result flushFrame(CGIF* pGIF, CGIF_Frame* pCur, CGIF_Frame* pBef) {
403416 transIndex = (1 << (pow2 + 1 )) - 1 ;
404417 }
405418 if (pTmpImageData == NULL ) {
406- pTmpImageData = malloc (MULU16 (imageWidth , imageHeight )); // TBD check return value of malloc
419+ pTmpImageData = malloc (MULU16 (imageWidth , imageHeight ));
420+ if (pTmpImageData == NULL ) {
421+ return CGIF_EALLOC ; // allocation failed
422+ }
407423 memcpy (pTmpImageData , pCur -> config .pImageData , MULU16 (imageWidth , imageHeight ));
408424 }
409425 pBefImageData = pBef -> config .pImageData ;
@@ -537,12 +553,27 @@ int cgif_addframe(CGIF* pGIF, CGIF_FrameConfig* pConfig) {
537553 }
538554 // create new Frame struct + make a deep copy of pConfig.
539555 pNewFrame = malloc (sizeof (CGIF_Frame ));
556+ if (pNewFrame == NULL ) {
557+ pGIF -> curResult = CGIF_EALLOC ;
558+ return pGIF -> curResult ;
559+ }
540560 copyFrameConfig (& (pNewFrame -> config ), pConfig );
541561 pNewFrame -> config .pImageData = malloc (MULU16 (pGIF -> config .width , pGIF -> config .height ));
562+ if (pNewFrame -> config .pImageData == NULL ) {
563+ free (pNewFrame );
564+ pGIF -> curResult = CGIF_EALLOC ;
565+ return pGIF -> curResult ;
566+ }
542567 memcpy (pNewFrame -> config .pImageData , pConfig -> pImageData , MULU16 (pGIF -> config .width , pGIF -> config .height ));
543568 // make a deep copy of the local color table, if required.
544569 if (pConfig -> attrFlags & CGIF_FRAME_ATTR_USE_LOCAL_TABLE ) {
545570 pNewFrame -> config .pLocalPalette = malloc (pConfig -> numLocalPaletteEntries * 3 );
571+ if (pNewFrame -> config .pLocalPalette == NULL ) {
572+ free (pNewFrame -> config .pImageData );
573+ free (pNewFrame );
574+ pGIF -> curResult = CGIF_EALLOC ;
575+ return pGIF -> curResult ;
576+ }
546577 memcpy (pNewFrame -> config .pLocalPalette , pConfig -> pLocalPalette , pConfig -> numLocalPaletteEntries * 3 );
547578 }
548579 pNewFrame -> disposalMethod = DISPOSAL_METHOD_LEAVE ;
0 commit comments