Fix all definitive memory leaks in valgrind

This commit is contained in:
Palanix 2024-04-07 00:46:14 +02:00
parent e270bf0e14
commit 26eac4f72b
No known key found for this signature in database
GPG key ID: 439CD6BD9905FAC5
2 changed files with 16 additions and 25 deletions

25
ccc.c
View file

@ -158,8 +158,7 @@ int main(int argc, char** argv)
/* prompt user so error message can be shown to user */
getch();
}
endwin();
return 0;
goto cleanup;
/* reload using z */
case 'z':
@ -388,6 +387,7 @@ int main(int argc, char** argv)
break;
}
}
cleanup:
free(argv_cp);
arraylist_free(files);
arraylist_free(marked);
@ -449,16 +449,10 @@ char *check_trash_dir()
*/
void change_dir(const char *buf, int selection, int ftype)
{
char *buf_dup;
if (buf == p_cwd) {
buf_dup = estrdup(p_cwd);
} else {
buf_dup = (char *) buf;
}
strcpy(cwd, buf_dup);
if (ftype != 2) {
if (cwd != buf)
strcpy(cwd, buf);
if (ftype == 0)
arraylist_free(files);
}
current_selection = selection;
populate_files(cwd, ftype);
}
@ -530,20 +524,19 @@ void populate_files(const char *path, int ftype)
}
while ((ep = readdir(dp)) != NULL) {
char *path = memalloc(PATH_MAX * sizeof(char));
char *filename = memalloc(PATH_MAX * sizeof(char));
/* copy filename */
strcpy(filename, ep->d_name);
char *filename = estrdup(ep->d_name);
/* use strncmp to filter out dotfiles */
if ((!show_hidden && strncmp(filename, ".", 1) && strncmp(filename, "..", 2)) || (show_hidden && strcmp(filename, ".") && strcmp(filename, ".."))) {
/* construct full file path */
char *path = memalloc((strlen(cwd) + strlen(filename) + 2) * sizeof(char));
strcpy(path, cwd);
strcat(path, "/");
strcat(path, filename); /* add filename */
add_file_stat(filename, path, ftype);
}
else free(filename);
}
if (ftype == 0) {
files = arraylist_init(tmp1->length + tmp2->length);
@ -552,6 +545,8 @@ void populate_files(const char *path, int ftype)
memcpy(files->items + tmp1->length, tmp2->items, tmp2->length * sizeof(file));
free(tmp1->items);
free(tmp2->items);
free(tmp1);
free(tmp2);
}
closedir(dp);
wrefresh(directory_content);

16
file.c
View file

@ -33,7 +33,7 @@ void arraylist_free(ArrayList *list)
}
free(list->items);
list->length = 0;
free(list);
}
/*
@ -113,15 +113,11 @@ void arraylist_add(ArrayList *list, char *name, char *path, char *stats, char *t
char *get_line(ArrayList *list, long index, bool detail, bool icons)
{
file file = list->items[index];
char *name = estrdup(file.name);
wchar_t *icon = ewcsdup(file.icon);
size_t name_len = strlen(name);
char *stats = NULL;
size_t name_len = strlen(file.name);
size_t length;
if (detail) {
stats = estrdup(file.stats);
length = name_len + strlen(stats) + 7; /* 4 for icon, 2 for space and 1 for null */
length = name_len + strlen(file.stats) + 7; /* 4 for icon, 2 for space and 1 for null */
} else {
length = name_len + 6; /* 4 for icon, 1 for space and 1 for null */
}
@ -129,17 +125,17 @@ char *get_line(ArrayList *list, long index, bool detail, bool icons)
char *line = memalloc(length * sizeof(char));
line[0] = '\0';
if (detail) {
strcat(line, stats);
strcat(line, file.stats);
strcat(line, " ");
}
if (icons) {
char *tmp = memalloc(8 * sizeof(char));
snprintf(tmp, 8, "%ls", icon);
snprintf(tmp, 8, "%ls", file.icon);
strcat(line, tmp);
strcat(line, " ");
free(tmp);
}
strcat(line, name);
strcat(line, file.name);
return line;
}