From 24ac6e32aa6eda21ea756add9b7d6d8466f5c76b Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sat, 7 Apr 2012 19:15:41 +0200 Subject: [PATCH] Bugfix: Properly terminate lines not ending with a newline (Thanks xeen) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, we didn’t check for a newline and thus could be corrupting formerly valid UTF-8 input, such as echo -n '↓' Fixes: #671 --- i3bar/src/child.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/i3bar/src/child.c b/i3bar/src/child.c index c97f5838..0b6f07df 100644 --- a/i3bar/src/child.c +++ b/i3bar/src/child.c @@ -153,7 +153,7 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) { int n = 0; int rec = 0; int buffer_len = STDIN_CHUNK_SIZE; - unsigned char *buffer = smalloc(buffer_len); + unsigned char *buffer = smalloc(buffer_len+1); buffer[0] = '\0'; while(1) { n = read(fd, buffer + rec, buffer_len - rec); @@ -217,7 +217,9 @@ void stdin_io_cb(struct ev_loop *loop, ev_io *watcher, int revents) { FREE(first->full_text); /* Remove the trailing newline and terminate the string at the same * time. */ - buffer[rec-1] = '\0'; + if (buffer[rec-1] == '\n' || buffer[rec-1] == '\r') + buffer[rec-1] = '\0'; + else buffer[rec] = '\0'; first->full_text = (char*)buffer; } draw_bars();