Fix initial buffer overflow

This commit is contained in:
Night Kaly 2025-01-16 20:39:08 +00:00
parent d6f9054bc0
commit 165852bf65
Signed by: night0721
SSH key fingerprint: SHA256:B/hgVwUoBpx5vdNsXl9w8XwZljA9766uk6T4ubZp5HM

33
file.c
View file

@ -105,25 +105,18 @@ char *get_line(ArrayList *list, long index, int detail, int icons)
{ {
file f = list->items[index]; file f = list->items[index];
size_t length = strlen(f.name) + 1; size_t length = strlen(f.name) + 1;
if (detail) { length += detail ? strlen(f.stats) + 1 : 0;
length += strlen(f.stats) + 1; /* 1 for space */ length += icons ? strlen(f.icon) + 1 : 0;
}
if (icons) {
length += 5; /* 4 for icon, 1 for space */
}
char *line = memalloc(length); char *line = memalloc(length);
line[0] = '\0';
if (detail) { snprintf(line, length, "%s%s%s%s%s",
strcat(line, f.stats); detail ? f.stats : "",
strcat(line, " "); detail ? " " : "",
} icons ? f.icon : "",
if (icons) { icons ? " " : "",
strcat(line, f.icon); f.name);
strcat(line, " ");
} return line;
strcat(line, f.name);
line[length] = '\0';
return line;
} }