This repository was archived by the owner on Apr 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 357
update memory allocate check #30
Open
aronlt
wants to merge
1
commit into
google:master
Choose a base branch
from
aronlt:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,6 +94,7 @@ typedef struct ListNode { | |
| // Debug information for malloc(). | ||
| typedef struct MallocBlockInfo { | ||
| void* block; // Address of the block returned by malloc(). | ||
| void* ptr; // Address returned to caller | ||
| size_t allocated_size; // Total size of the allocated block. | ||
| size_t size; // Request block size. | ||
| SourceLocation location; // Where the block was allocated. | ||
|
|
@@ -1361,6 +1362,7 @@ void* _test_malloc(const size_t size, const char* file, const int line) { | |
| block_info->allocated_size = allocate_size; | ||
| block_info->size = size; | ||
| block_info->block = block; | ||
| block_info->ptr = ptr; | ||
| block_info->node.value = block_info; | ||
| list_add(block_list, &block_info->node); | ||
| return ptr; | ||
|
|
@@ -1377,6 +1379,22 @@ void* _test_calloc(const size_t number_of_elements, const size_t size, | |
| return ptr; | ||
| } | ||
|
|
||
| //Chech if the address is really allocated | ||
| int check_allocated_exist(void* const ptr){ | ||
| const ListNode * const head = get_allocated_blocks_list(); | ||
| const ListNode *node = head->next; | ||
| int rc = 0; | ||
| while(node != head){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We roughly follow google C++ style https://google.github.io/styleguide/cppguide.html in here. So this should have some whitespace... "while (node != head) {"
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same sort of this below with the "if" statements |
||
| const MallocBlockInfo * const block_info = (const MallocBlockInfo*)node->value; | ||
| assert_true(block_info); | ||
| if(block_info->ptr == ptr){ | ||
| rc = 1; | ||
| break; | ||
| } | ||
| node = node->next; | ||
| } | ||
| return rc; | ||
| } | ||
|
|
||
| // Use the real free in this function. | ||
| #undef free | ||
|
|
@@ -1385,6 +1403,17 @@ void _test_free(void* const ptr, const char* file, const int line) { | |
| char *block = (char*)ptr; | ||
| MallocBlockInfo *block_info; | ||
| _assert_true((int)ptr, "ptr", file, line); | ||
|
|
||
| //is it a allocated block(maybe free twice) | ||
| if(!check_allocated_exist((void * const)ptr)){ | ||
| print_error( | ||
| "the address of 0x%08x in " | ||
| SOURCE_LOCATION_FORMAT " is not a allocated block\n", | ||
| (size_t)ptr, file, line | ||
| ); | ||
| _fail(file, line); | ||
| } | ||
|
|
||
| block_info = (MallocBlockInfo*)(block - (MALLOC_GUARD_SIZE + | ||
| sizeof(*block_info))); | ||
| // Check the guard blocks. | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ | |
| #endif | ||
| #include <sys/types.h> | ||
|
|
||
| #if UNIT_TESTING | ||
| //#if UNIT_TESTING | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to keep this in place so that the wrapper for the allocator is only in place when testing. |
||
| extern void* _test_malloc(const size_t size, const char* file, const int line); | ||
| extern void* _test_calloc(const size_t number_of_elements, const size_t size, | ||
| const char* file, const int line); | ||
|
|
@@ -30,13 +30,24 @@ extern void _test_free(void* const ptr, const char* file, const int line); | |
| #define malloc(size) _test_malloc(size, __FILE__, __LINE__) | ||
| #define calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) | ||
| #define free(ptr) _test_free(ptr, __FILE__, __LINE__) | ||
| #endif // UNIT_TESTING | ||
| //#endif // UNIT_TESTING | ||
|
|
||
| void leak_memory() { | ||
| int * const temporary = (int*)malloc(sizeof(int)); | ||
| *temporary = 0; | ||
| } | ||
|
|
||
| void free_bad_memory() { | ||
| int counter = 0; | ||
| free(&counter); | ||
| } | ||
|
|
||
| void free_twice_memory() { | ||
| int* temporary = (int*)malloc(sizeof(int)); | ||
| free(temporary); | ||
| free(temporary); | ||
| } | ||
|
|
||
| void buffer_overflow() { | ||
| char * const memory = (char*)malloc(sizeof(int)); | ||
| memory[sizeof(int)] = '!'; | ||
|
|
||
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
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.
Chech --> Check