tests/malloc: fix of dereferencing a NULL pointer

If the memory is exhausted during the allocation of the new `head` structure, subsequent accesses to `head` will result in dereferencing of a NULL pointer.
This commit is contained in:
Gunar Schorcht 2019-03-23 14:01:28 +01:00
parent c391ed4109
commit 136849661a

View File

@ -38,8 +38,10 @@ void fill_memory(struct node *head)
printf("Allocated %d Bytes at 0x%p, total %d\n", CHUNK_SIZE, head->ptr, total += CHUNK_SIZE);
memset(head->ptr, '@', CHUNK_SIZE);
head = head->next = malloc(sizeof(struct node));
head->ptr = 0;
head->next = 0;
if (head) {
head->ptr = 0;
head->next = 0;
}
total += sizeof(struct node);
}
}