Skip to content

Commit

Permalink
Hook calloc internally
Browse files Browse the repository at this point in the history
At present, the following C functions are hooked in header "harness.h":
- malloc
- calloc
- free
- strdup (non-C standard, but valid in POSIX.1)
  • Loading branch information
jserv committed Jan 25, 2020
1 parent d2d7711 commit b42797a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
12 changes: 12 additions & 0 deletions harness.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ void *test_malloc(size_t size)
return p;
}

// cppcheck-suppress unusedFunction
void *test_calloc(size_t nelem, size_t elsize)
{
/* Reference: Malloc tutorial
* https://danluu.com/malloc-tutorial/
*/
size_t size = nelem * elsize; // TODO: check for overflow
void *ptr = test_malloc(size);
memset(ptr, 0, size);
return ptr;
}

void test_free(void *p)
{
if (noallocate_mode) {
Expand Down
1 change: 1 addition & 0 deletions harness.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

void *test_malloc(size_t size);
void *test_calloc(size_t nmemb, size_t size);
void test_free(void *p);
char *test_strdup(const char *s);
/* FIXME: provide test_realloc as well */
Expand Down

0 comments on commit b42797a

Please sign in to comment.