
Test your allocs protections and leaks ! Report Bug · Request Feature
Table of Contents
About The Tool
This tool allows you to test if every allocs in your project are protected. It also checks if an alloc fail: if you free every allocations.
It also check leaks !
Getting Started
Here, you will see how to setup this tool in a example project.
Prerequisites
This is working out of the box on 42's VM
If your are not on the VM you will need those installed on your system.
Your CC must be clang
- Linux
- clang
- llvm
Quickstart
- Clone the repo inside your project
git clone https://github.com/tmatis/ft_mallocator.git
- Cd in directory
cd ./ft_mallocator
- Execute script
bash test.sh
- Follow script's instructions...
Demo video
record.mp4
Usage
Here we have an example of not well protected code:
char *malloc_function(void)
{
return (malloc(1000));
}
int main(void)
{
void *ptr = malloc_function();
if (ptr == NULL)
return (1);
free(ptr);
void *array[10];
for (int i = 0; i < 10; i++)
{
array[i] = malloc(1000);
if (array[i] == NULL)
{
printf("it fail %i\n", i);
return (0);
}
memset(array[i], 0, 1000);
}
for (int i = 0; i < 10; i++)
free(array[i]);
ptr = malloc_function();
memset(ptr, 0, 1000);
free(ptr);
}
this exemples have many problems, let's run mallocator on it...
The first allocation is well protected nothing to say. The second alloction if protected but if a malloc crash not everythings is free. (At the second iteration of the for loop) The third allocation is not protected at all, there is no null check.
Let's see a well protected example..
char *malloc_function(void)
{
return (malloc(1000));
}
int main(void)
{
void *ptr = malloc_function();
if (ptr == NULL)
return (1);
free(ptr);
void *array[10];
int i = 0;
for (i = 0; i < 10; i++)
{
array[i] = malloc(1000);
if (array[i] == NULL)
{
printf("it fail %i\n", i);
break ;
}
memset(array[i], 0, 1000);
}
for (int j = 0; j < i; j++)
free(array[j]);
ptr = malloc_function();
if (!ptr)
return (1);
memset(ptr, 0, 1000);
free(ptr);
}
Then we run mallocator on this code ...
This code is protected.
You can see how it is working behind the scene .
Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Contact
- Théo Matis [email protected]