|
| 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 | +} |
0 commit comments