Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

froz42/ft_mallocator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

THIS PROJECT IS DEPRECATED AND NO LONGER MAINTAINED

This project is deprecated in profit of funcheck which is more easy to use and more powerful.


Logo

Test your allocs protections and leaks ! Report Bug · Request Feature

Table of Contents
  1. About The Tool
  2. Getting Started
  3. Usage
  4. Contributing
  5. Contact

About The Tool

screenshot

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 allocation.

It also checks leaks !

(back to top)

Getting Started

Here, you will see how to setup this tool in an example project.

Prerequisites

This is working out of the box on 42's dump.

You must compile using clang

At home, you will need those installed on your system :

sudo apt install make clang

Quickstart

  1. Clone the repo inside your project
    git clone https://github.com/tmatis/ft_mallocator.git
  2. Cd in directory
    cd ./ft_mallocator
  3. Execute script
    bash test.sh
  4. Follow script's instructions...

Demo video

record.mp4

(back to top)

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 example have many problems, let's run mallocator on it : screenshot

The first allocation is well protected, nothing to say. The second allocation is protected but if a malloc fails, not everything is freed (the program returns without freeing the previous allocations). 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 :

screenshot

This code is protected.

You can see how it is working behind the scene here.

(back to top)

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

Contact

tmatis's 42 stats

(back to top)

About

Test your malloc protection

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published