From 165852bf65da26bb3f9d319752aaf72095767f85 Mon Sep 17 00:00:00 2001 From: night0721 <night@night0721.xyz> Date: Thu, 16 Jan 2025 20:39:08 +0000 Subject: [PATCH] Fix initial buffer overflow --- file.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/file.c b/file.c index 4846155..3091abb 100644 --- a/file.c +++ b/file.c @@ -105,25 +105,18 @@ char *get_line(ArrayList *list, long index, int detail, int icons) { file f = list->items[index]; - size_t length = strlen(f.name) + 1; - if (detail) { - length += strlen(f.stats) + 1; /* 1 for space */ - } - if (icons) { - length += 5; /* 4 for icon, 1 for space */ - } + size_t length = strlen(f.name) + 1; + length += detail ? strlen(f.stats) + 1 : 0; + length += icons ? strlen(f.icon) + 1 : 0; - char *line = memalloc(length); - line[0] = '\0'; - if (detail) { - strcat(line, f.stats); - strcat(line, " "); - } - if (icons) { - strcat(line, f.icon); - strcat(line, " "); - } - strcat(line, f.name); - line[length] = '\0'; - return line; + char *line = memalloc(length); + + snprintf(line, length, "%s%s%s%s%s", + detail ? f.stats : "", + detail ? " " : "", + icons ? f.icon : "", + icons ? " " : "", + f.name); + + return line; }