From c1933dc186db0c590588c2f63759d72905e74ac6 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Tue, 23 Nov 2010 17:33:26 +0100 Subject: [PATCH 1/2] Make sure char-arrays filled with read() are null-terminated --- src/conky.cc | 4 ++-- src/linux.cc | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index 0bc50212..cf296d7a 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -1988,7 +1988,7 @@ static void main_loop(void) #ifdef HAVE_SYS_INOTIFY_H int inotify_config_wd = -1; #define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event)) -#define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16)) +#define INOTIFY_BUF_LEN (20 * (INOTIFY_EVENT_SIZE + 16)) + 1 char inotify_buff[INOTIFY_BUF_LEN]; #endif /* HAVE_SYS_INOTIFY_H */ @@ -2408,7 +2408,7 @@ static void main_loop(void) select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait); if (FD_ISSET(inotify_fd, &descriptors)) { /* process inotify events */ - len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN); + len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN - 1); while (len > 0 && idx < len) { struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx]; if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) { diff --git a/src/linux.cc b/src/linux.cc index 46cc42a6..6cb16d6c 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -2454,7 +2454,7 @@ static unsigned long long calc_cpu_total(void) KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGPROC : TMPL_SHORTPROC; ps = open("/proc/stat", O_RDONLY); - rc = read(ps, line, sizeof(line)); + rc = read(ps, line, BUFFER_LEN - 1); close(ps); if (rc < 0) { return 0; @@ -2535,7 +2535,7 @@ static void process_parse_stat(struct process *process) /* Mark process as up-to-date. */ process->time_stamp = g_time; - rc = read(ps, line, sizeof(line)); + rc = read(ps, line, BUFFER_LEN - 1); close(ps); if (rc < 0) { return; @@ -2572,11 +2572,9 @@ static void process_parse_stat(struct process *process) return; } - endl = read(ps, line, sizeof(line)); + endl = read(ps, line, BUFFER_LEN - 1); close(ps); - /* null terminate the input */ - line[endl] = 0; /* account for "kdeinit: " */ if ((char *) line == strstr(line, "kdeinit: ")) { r = ((char *) line) + 9; @@ -2647,7 +2645,7 @@ static void process_parse_io(struct process *process) return; } - rc = read(ps, line, sizeof(line)); + rc = read(ps, line, BUFFER_LEN - 1); close(ps); if (rc < 0) { return; From 23a193fe89c9aa8aa100eca1c9bbfd610fae53f8 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Tue, 23 Nov 2010 17:40:53 +0100 Subject: [PATCH 2/2] Complete previous commit --- src/conky.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/conky.cc b/src/conky.cc index cf296d7a..33b0726e 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -2409,6 +2409,7 @@ static void main_loop(void) if (FD_ISSET(inotify_fd, &descriptors)) { /* process inotify events */ len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN - 1); + inotify_buff[len] = 0; while (len > 0 && idx < len) { struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx]; if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) {