From 96e14d81032cb53c56664e88816f57efd24c415b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fernando=20Tarl=C3=A1=20Cardoso=20Lemos?= Date: Sun, 2 Jan 2011 22:39:49 -0200 Subject: [PATCH] Separate the lines received in a single read. Fixes the case where multiple lines are read in a single read syscall (it could be better optimized in the future). Also fixes a memory corruption issue when rec == 0. --- i3bar/include/common.h | 1 + i3bar/src/child.c | 23 ++++++++++++++++------- i3bar/src/main.c | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/i3bar/include/common.h b/i3bar/include/common.h index 4ad33703..8b439ee0 100644 --- a/i3bar/include/common.h +++ b/i3bar/include/common.h @@ -14,6 +14,7 @@ typedef int bool; struct ev_loop* main_loop; char *statusline; +char *statusline_buffer; struct rect_t { int x; diff --git a/i3bar/src/child.c b/i3bar/src/child.c index d7d55783..1fae7593 100644 --- a/i3bar/src/child.c +++ b/i3bar/src/child.c @@ -27,6 +27,9 @@ pid_t child_pid; ev_io *stdin_io; ev_child *child_sig; +/* The buffer statusline points to */ +char *statusline_buffer = NULL; + /* * Stop and free() the stdin- and sigchild-watchers * @@ -36,7 +39,7 @@ void cleanup() { ev_child_stop(main_loop, child_sig); FREE(stdin_io); FREE(child_sig); - FREE(statusline); + FREE(statusline_buffer); } /* @@ -50,7 +53,7 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) { int rec = 0; int buffer_len = STDIN_CHUNK_SIZE; char *buffer = malloc(buffer_len); - memset(buffer, '\0', buffer_len); + buffer[0] = '\0'; while(1) { n = read(fd, buffer + rec, buffer_len - rec); if (n == -1) { @@ -67,8 +70,10 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) { buffer_len += STDIN_CHUNK_SIZE; buffer = realloc(buffer, buffer_len); } else { - /* remove trailing newline and finish up */ - buffer[rec-1] = '\0'; + if (rec != 0) { + /* remove trailing newline and finish up */ + buffer[rec-1] = '\0'; + } break; } } @@ -78,9 +83,13 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) { FREE(buffer); return; } - FREE(statusline); - statusline = buffer; - DLOG("%s\n", buffer); + FREE(statusline_buffer); + statusline = statusline_buffer = buffer; + for (n = 0; buffer[n] != '\0'; ++n) { + if (buffer[n] == '\n') + statusline = &buffer[n + 1]; + } + DLOG("%s\n", statusline); draw_bars(); } diff --git a/i3bar/src/main.c b/i3bar/src/main.c index c2cf53e2..fce7fc29 100644 --- a/i3bar/src/main.c +++ b/i3bar/src/main.c @@ -249,7 +249,7 @@ int main(int argc, char **argv) { FREE(socket_path); - FREE(statusline); + FREE(statusline_buffer); clean_xcb(); ev_default_destroy(); -- 2.39.5