Skip to content

Commit 74950ce

Browse files
authored
Merge branch 'main' into ealloc_test
2 parents c3c5eac + dcd45c7 commit 74950ce

3 files changed

Lines changed: 106 additions & 0 deletions

File tree

src/cgif_raw.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ static int LZW_GenerateStream(LZWResult* pResult, const uint32_t numPixel, const
308308
if(pContext == NULL) {
309309
return CGIF_EALLOC;
310310
}
311+
memset(pContext, 0, sizeof(LZWGenState));
311312
pContext->pTreeInit = malloc((initDictLen * sizeof(uint16_t)) * initDictLen);
312313
if(pContext->pTreeInit == NULL) {
313314
r = CGIF_EALLOC;

tests/ealloc_raw.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <stdlib.h>
2+
#include <stdint.h>
3+
#include <string.h>
4+
#include <stdio.h>
5+
6+
#include "cgif_raw.h"
7+
8+
#define WIDTH 2
9+
#define HEIGHT 2
10+
11+
/* malloc failure injection */
12+
static int fail_after;
13+
static int malloc_count;
14+
15+
static void* cgif_test_malloc(size_t size) {
16+
if(fail_after > 0) {
17+
++malloc_count;
18+
if(malloc_count == fail_after) {
19+
return NULL;
20+
}
21+
}
22+
return malloc(size);
23+
}
24+
25+
/* redirect malloc calls inside cgif_raw.c to our wrapper */
26+
#define malloc(s) cgif_test_malloc(s)
27+
#include "../src/cgif_raw.c"
28+
#undef malloc
29+
30+
/* no-op write callback */
31+
static int writeFn(void* pContext, const uint8_t* pData, const size_t numBytes) {
32+
(void)pContext;
33+
(void)pData;
34+
(void)numBytes;
35+
return 0;
36+
}
37+
38+
int main(void) {
39+
CGIFRaw* pGIF;
40+
CGIFRaw_Config gConfig;
41+
CGIFRaw_FrameConfig fConfig;
42+
cgif_result r;
43+
uint8_t aPalette[] = {
44+
0x00, 0x00, 0x00, // black
45+
0xFF, 0xFF, 0xFF, // white
46+
};
47+
uint8_t aImageData[WIDTH * HEIGHT];
48+
memset(aImageData, 0, sizeof(aImageData));
49+
50+
for(fail_after = 1; ; ++fail_after) {
51+
memset(&gConfig, 0, sizeof(gConfig));
52+
memset(&fConfig, 0, sizeof(fConfig));
53+
gConfig.pWriteFn = writeFn;
54+
gConfig.width = WIDTH;
55+
gConfig.height = HEIGHT;
56+
gConfig.pGCT = aPalette;
57+
gConfig.sizeGCT = 2;
58+
59+
fConfig.pImageData = aImageData;
60+
fConfig.width = WIDTH;
61+
fConfig.height = HEIGHT;
62+
fConfig.attrFlags = CGIF_RAW_FRAME_ATTR_INTERLACED;
63+
64+
malloc_count = 0;
65+
66+
pGIF = cgif_raw_newgif(&gConfig);
67+
if(pGIF == NULL) {
68+
if(malloc_count >= fail_after) {
69+
continue; // expected: our injected malloc failure
70+
}
71+
fputs("unexpected NULL from cgif_raw_newgif\n", stderr);
72+
return 1;
73+
}
74+
75+
r = cgif_raw_addframe(pGIF, &fConfig);
76+
if(r != CGIF_OK) {
77+
if(r == CGIF_EALLOC && malloc_count >= fail_after) {
78+
cgif_raw_close(pGIF);
79+
continue; // expected: our injected malloc failure
80+
}
81+
fprintf(stderr, "unexpected error from cgif_raw_addframe: %d\n", r);
82+
return 1;
83+
}
84+
85+
r = cgif_raw_close(pGIF);
86+
if(malloc_count < fail_after) {
87+
// all mallocs succeeded without hitting our failure point -- done
88+
if(r != CGIF_OK) {
89+
fprintf(stderr, "unexpected error from cgif_raw_close: %d\n", r);
90+
return 1;
91+
}
92+
break;
93+
}
94+
}
95+
96+
return 0;
97+
}

tests/meson.build

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,21 @@ foreach t : tests_index + tests_rgb
7575
test(name, test_exe, priority : 0)
7676
endforeach
7777

78+
# malloc failure tests (compile source directly to intercept malloc)
7879
test_ealloc_exe = executable(
7980
'test_ealloc',
8081
'ealloc.c',
8182
include_directories : ['../inc/'],
8283
)
8384
test('ealloc', test_ealloc_exe, priority : 0)
8485

86+
test_ealloc_raw_exe = executable(
87+
'test_ealloc_raw',
88+
'ealloc_raw.c',
89+
include_directories : ['../inc/'],
90+
)
91+
test('ealloc_raw', test_ealloc_raw_exe, priority : 0)
92+
8593
sha256sumc = find_program('scripts/sha256sum.py')
8694
# get the ordering right:
8795
# md5sum check on output GIFs should be run once all of the above tests are done.

0 commit comments

Comments
 (0)