-
Notifications
You must be signed in to change notification settings - Fork 26
add malloc failure test for cgif_raw API #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| #include <stdlib.h> | ||
| #include <stdint.h> | ||
| #include <string.h> | ||
| #include <stdio.h> | ||
|
|
||
| #include "cgif_raw.h" | ||
|
|
||
| #define WIDTH 2 | ||
| #define HEIGHT 2 | ||
|
|
||
| /* malloc failure injection */ | ||
| static int fail_after = 0; | ||
| static int malloc_count = 0; | ||
|
|
||
| static void* cgif_test_malloc(size_t size) { | ||
| if(fail_after > 0) { | ||
| ++malloc_count; | ||
| if(malloc_count == fail_after) { | ||
| return NULL; | ||
| } | ||
| } | ||
| return malloc(size); | ||
| } | ||
|
|
||
| /* redirect malloc calls inside cgif_raw.c to our wrapper */ | ||
| #define malloc(s) cgif_test_malloc(s) | ||
| #include "../src/cgif_raw.c" | ||
| #undef malloc | ||
|
|
||
| /* no-op write callback */ | ||
| static int writeFn(void* pContext, const uint8_t* pData, const size_t numBytes) { | ||
| (void)pContext; | ||
| (void)pData; | ||
| (void)numBytes; | ||
| return 0; | ||
| } | ||
|
|
||
| int main(void) { | ||
| CGIFRaw* pGIF; | ||
| CGIFRaw_Config gConfig; | ||
| CGIFRaw_FrameConfig fConfig; | ||
| cgif_result r; | ||
| uint8_t aPalette[] = { | ||
| 0x00, 0x00, 0x00, // black | ||
| 0xFF, 0xFF, 0xFF, // white | ||
| }; | ||
| uint8_t aImageData[WIDTH * HEIGHT]; | ||
| memset(aImageData, 0, sizeof(aImageData)); | ||
|
|
||
| for(int n = 1; ; ++n) { | ||
| memset(&gConfig, 0, sizeof(gConfig)); | ||
| memset(&fConfig, 0, sizeof(fConfig)); | ||
| gConfig.pWriteFn = writeFn; | ||
| gConfig.width = WIDTH; | ||
| gConfig.height = HEIGHT; | ||
| gConfig.pGCT = aPalette; | ||
| gConfig.sizeGCT = 2; | ||
|
|
||
| fConfig.pImageData = aImageData; | ||
| fConfig.width = WIDTH; | ||
| fConfig.height = HEIGHT; | ||
|
|
||
| fail_after = n; | ||
| malloc_count = 0; | ||
|
|
||
| pGIF = cgif_raw_newgif(&gConfig); | ||
| if(pGIF == NULL) { | ||
| if(malloc_count >= n) { | ||
| continue; // expected: our injected malloc failure | ||
| } | ||
| fputs("unexpected NULL from cgif_raw_newgif\n", stderr); | ||
| return 1; | ||
| } | ||
|
|
||
| r = cgif_raw_addframe(pGIF, &fConfig); | ||
| if(r != CGIF_OK) { | ||
| if(r == CGIF_EALLOC && malloc_count >= n) { | ||
| cgif_raw_close(pGIF); | ||
| continue; // expected: our injected malloc failure | ||
| } | ||
| fprintf(stderr, "unexpected error from cgif_raw_addframe: %d\n", r); | ||
| return 1; | ||
| } | ||
|
|
||
| r = cgif_raw_close(pGIF); | ||
| if(malloc_count < n) { | ||
| // all mallocs succeeded without hitting our failure point -- done | ||
| if(r != CGIF_OK) { | ||
| fprintf(stderr, "unexpected error from cgif_raw_close: %d\n", r); | ||
| return 1; | ||
| } | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return 0; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found by the new
ealloc_rawtest with ASAN:When an early malloc fails, the goto cleanup path frees all pContext fields, including ones never assigned. Zero-initialize pContext so unassigned pointers are
NULLand safe to free.