Skip to content

Commit

Permalink
Merge pull request #41 from dloebl/fix-earlyclose
Browse files Browse the repository at this point in the history
Fix potential memory leak when closing a GIF stream without frames
  • Loading branch information
dloebl authored Feb 7, 2022
2 parents 6b8737f + 2116cc5 commit aac10e5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/cgif.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,14 +452,15 @@ int cgif_close(CGIF* pGIF) {
}
}
}

// cleanup
CGIF_CLOSE_Cleanup:
r = cgif_raw_close(pGIF->pGIFRaw); // close raw GIF stream
// check for errors
if(r != CGIF_OK) {
pGIF->curResult = r;
}

// cleanup
CGIF_CLOSE_Cleanup:
if(pGIF->pFile) {
r = fclose(pGIF->pFile); // we are done at this point => close the file
if(r) {
Expand Down
55 changes: 55 additions & 0 deletions tests/earlyclose.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>

#include "cgif.h"

#define WIDTH 100
#define HEIGHT 100

static int pWriteFn(void* pContext, const uint8_t* pData, const size_t numBytes) {
(void)pContext;
(void)pData;
(void)numBytes;
// just ignore GIF data
return 0;
}

int main(void) {
CGIF* pGIF;
CGIF_Config gConfig;
CGIF_FrameConfig fConfig;
uint8_t* pImageData;
uint8_t aPalette[] = {
0x00, 0x00, 0x00, // black
0xFF, 0xFF, 0xFF, // white
};
cgif_result r;

memset(&gConfig, 0, sizeof(CGIF_Config));
memset(&fConfig, 0, sizeof(CGIF_FrameConfig));
gConfig.width = WIDTH;
gConfig.height = HEIGHT;
gConfig.pGlobalPalette = aPalette;
gConfig.numGlobalPaletteEntries = 2;
gConfig.pWriteFn = pWriteFn;
gConfig.pContext = NULL;
//
// create new GIF
pGIF = cgif_newgif(&gConfig);
if(pGIF == NULL) {
fputs("failed to create new GIF via cgif_newgif()\n", stderr);
return 1;
}
//
// close GIF without adding frames before
r = cgif_close(pGIF); // free allocated space at the end of the session

// check for correct error
if(r != CGIF_OK) {
return 0;
}
fputs("CGIF_ERROR expected as result code\n", stderr);
return 2;
}
1 change: 1 addition & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ tests = [
'animated_stripes_horizontal',
'animated_stripe_pattern',
'animated_stripe_pattern_2',
'earlyclose',
'eindex',
'ewrite',
'global_plus_local_table',
Expand Down

0 comments on commit aac10e5

Please sign in to comment.