1919#define HEIGHT 16
2020
2121/* malloc/realloc failure injection */
22- static int fail_after = 0 ;
23- static int malloc_count = 0 ;
22+ static int fail_after ;
23+ static int malloc_count ;
2424
2525static void * cgif_test_malloc (size_t size ) {
2626 if (fail_after > 0 ) {
@@ -94,7 +94,7 @@ int main(void) {
9494 uint8_t aImageData [WIDTH * HEIGHT * 3 ];
9595 initImageData (aImageData , WIDTH * HEIGHT );
9696
97- for (int n = 1 ; ; ++ n ) {
97+ for (fail_after = 1 ; ; ++ fail_after ) {
9898 memset (& gConfig , 0 , sizeof (gConfig ));
9999 memset (& fConfig , 0 , sizeof (fConfig ));
100100 gConfig .pWriteFn = writeFn ;
@@ -105,12 +105,11 @@ int main(void) {
105105 fConfig .fmtChan = CGIF_CHAN_FMT_RGB ;
106106 fConfig .attrFlags = CGIF_RGB_FRAME_ATTR_INTERLACED ;
107107
108- fail_after = n ;
109108 malloc_count = 0 ;
110109
111110 pGIF = cgif_rgb_newgif (& gConfig );
112111 if (pGIF == NULL ) {
113- if (malloc_count >= n ) {
112+ if (malloc_count >= fail_after ) {
114113 continue ; // expected: our injected malloc failure
115114 }
116115 fputs ("unexpected NULL from cgif_rgb_newgif\n" , stderr );
@@ -119,7 +118,7 @@ int main(void) {
119118
120119 r = cgif_rgb_addframe (pGIF , & fConfig );
121120 if (r != CGIF_OK ) {
122- if (r == CGIF_EALLOC && malloc_count >= n ) {
121+ if (r == CGIF_EALLOC && malloc_count >= fail_after ) {
123122 cgif_rgb_close (pGIF );
124123 continue ; // expected: our injected malloc failure
125124 }
@@ -128,16 +127,19 @@ int main(void) {
128127 }
129128
130129 r = cgif_rgb_close (pGIF );
131- if (malloc_count < n ) {
130+ if (malloc_count < fail_after ) {
132131 // all mallocs succeeded without hitting our failure point -- done
133132 if (r != CGIF_OK ) {
134133 fprintf (stderr , "unexpected error from cgif_rgb_close: %d\n" , r );
135134 return 1 ;
136135 }
137136 break ;
138137 }
139- // our injected failure was hit during close
140- continue ;
138+ // our injected failure was hit during close (e.g. LZW encoding)
139+ if (r != CGIF_EALLOC ) {
140+ fprintf (stderr , "expected CGIF_EALLOC from cgif_rgb_close, got: %d (fail_after=%d)\n" , r , fail_after );
141+ return 1 ;
142+ }
141143 }
142144
143145 return 0 ;
0 commit comments