Skip to content

Commit

Permalink
memset cause core dump
Browse files Browse the repository at this point in the history
if malloc fails, memset will catch exception cuz ptr pointer is NULL
  • Loading branch information
ByteMansion authored Jun 27, 2019
1 parent 4aa18f4 commit e7ade10
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion chapter2/code/calloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ void* another_calloc(size_t nmemb, size_t size) {
/* a good way to check overflow or not */
if (nmemb == buf_size / size) {
void* ptr = malloc(buf_size);
memset(ptr, 0, buf_size);
if(ptr != NULL) {
memset(ptr, 0, buf_size);
}
return ptr;
}
return NULL;
Expand Down

0 comments on commit e7ade10

Please sign in to comment.