dir_whole_partition_copy_aux bug ? Topic is solved

Using TestDisk to undelete files
Forum rules
When asking for technical support:
- Search for posts on the same topic before posting a new question.
- Give clear, specific information in the title of your post.
- Include as many details as you can, MOST POSTS WILL GET ONLY ONE OR TWO ANSWERS.
- Post a follow up with a "Thank you" or "This worked!"
- When you learn something, use that knowledge to HELP ANOTHER USER LATER.
Before posting, please read https://www.cgsecurity.org/testdisk.pdf
Locked
Message
Author
mesajflaviu
Posts: 37
Joined: 12 Sep 2019, 19:54

dir_whole_partition_copy_aux bug ?

#1 Post by mesajflaviu »

I have tested dir_whole_partition_copy_aux function, which is recursive. And using it, I got memory leaks:

Code: Select all

static int dir_whole_partition_copy_aux(disk_t *disk, const partition_t *partition, dir_data_t *dir_data, const unsigned long int inode, unsigned int *copy_ok, unsigned int *copy_bad)
{
	struct td_list_head *file_walker = NULL;
	static unsigned int dir_nbr = 0;
	static unsigned long int inode_known[MAX_DIR_NBR];
	const unsigned int current_directory_namelength = strlen(dir_data->current_directory);
	file_info_t dir_list;
	TD_INIT_LIST_HEAD(&dir_list.list);
	if (dir_nbr == MAX_DIR_NBR)
		return 1;	/* subdirectories depth is too high => Back */
	dir_data->get_dir(disk, partition, dir_data, inode, &dir_list);
	/* Not perfect for FAT32 root cluster */
	inode_known[dir_nbr++] = inode;
	td_list_for_each(file_walker, &dir_list.list)
	{
		const file_info_t *current_file = td_list_entry_const(file_walker, const file_info_t, list);
		if (strlen(dir_data->current_directory) + 1 + strlen(current_file->name) <
			sizeof(dir_data->current_directory) - 1)
		{
			if (strcmp(dir_data->current_directory, "/"))
				strcat(dir_data->current_directory, "/");
			strcat(dir_data->current_directory, current_file->name);
			if (LINUX_S_ISDIR(current_file->st_mode) != 0)
			{
				if (is_inode_valid(current_file, dir_nbr, inode_known) > 0)
				{
					dir_whole_partition_copy_aux(disk, partition, dir_data, current_file->st_ino, copy_ok, copy_bad);  // if I comment this line I get rid of memory leaks
				}
			}
			else if (LINUX_S_ISREG(current_file->st_mode) != 0)
			{
				if (dir_data->copy_file(disk, partition, dir_data, current_file) == 0)
					(*copy_ok)++;
				else
					(*copy_bad)++;
			}
		}
		/* restore current_directory name */
		dir_data->current_directory[current_directory_namelength] = '\0';
	}
	delete_list_file(&dir_list);
	dir_nbr--;
	return 0;
}
dir_list is loaded by data in line:

Code: Select all

dir_data->get_dir(disk, partition, dir_data, inode, &dir_list);
I noticed that dir_list has name member, which must be freed. and also, dir_list must be freed. Which is freed. But if I comment line

Code: Select all

dir_whole_partition_copy_aux(disk, partition, dir_data, current_file->st_ino, copy_ok, copy_bad);
I get rid of memory leaks ... and that give me a question: isn't a bug here ? The author of this code could be check this function ?
mesajflaviu
Posts: 37
Joined: 12 Sep 2019, 19:54

Re: dir_whole_partition_copy_aux bug ?

#2 Post by mesajflaviu »

I have solved.
Locked