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

16
file.c
View file

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