From dbda5d35eb448a7c2f846c3ade8c9659be9928ef Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Wed, 19 Sep 2018 15:16:19 +0100 Subject: [PATCH 1/3] swaybar: update buffer_pos when moving object to start of buffer --- swaybar/i3bar.c | 1 + 1 file changed, 1 insertion(+) diff --git a/swaybar/i3bar.c b/swaybar/i3bar.c index 88404703..4ea9c4c1 100644 --- a/swaybar/i3bar.c +++ b/swaybar/i3bar.c @@ -204,6 +204,7 @@ bool i3bar_handle_readable(struct status_line *status) { status->buffer_index -= buffer_pos; memmove(status->buffer, &status->buffer[buffer_pos], status->buffer_index); + buffer_pos = 0; } else { // expand buffer status->buffer_size *= 2; From ccdec2a329794356fdf559560261e5626a7e2098 Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Fri, 21 Sep 2018 14:59:01 +0100 Subject: [PATCH 2/3] swaybar: reset tokener if json is incomplete If the tokener parses incomplete json, it sets its error value to json_tokener_continue. This means that extra json should be provided, but the code was providing the entire object again. In the interest of simplicity, the tokener is reset so that buffer_pos always points to the start of the current object. --- swaybar/i3bar.c | 1 + 1 file changed, 1 insertion(+) diff --git a/swaybar/i3bar.c b/swaybar/i3bar.c index 4ea9c4c1..02b68a2a 100644 --- a/swaybar/i3bar.c +++ b/swaybar/i3bar.c @@ -199,6 +199,7 @@ bool i3bar_handle_readable(struct status_line *status) { } buffer_pos = status->buffer_index = 0; } else if (json_tokener_get_error(status->tokener) == json_tokener_continue) { + json_tokener_reset(status->tokener); if (status->buffer_index < status->buffer_size) { // move the object to the start of the buffer status->buffer_index -= buffer_pos; From a2326661e190bd759dc0a99f34a3d69dcdf8dc57 Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Fri, 21 Sep 2018 12:06:25 +0100 Subject: [PATCH 3/3] swaybar: log invalid i3bar json --- swaybar/i3bar.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/swaybar/i3bar.c b/swaybar/i3bar.c index 02b68a2a..325aa61a 100644 --- a/swaybar/i3bar.c +++ b/swaybar/i3bar.c @@ -117,7 +117,9 @@ bool i3bar_handle_readable(struct status_line *status) { memmove(status->buffer, &status->buffer[c], status->buffer_index); break; } else if (!isspace(status->buffer[c])) { - status_error(status, "[invalid json]"); + wlr_log(WLR_DEBUG, "Invalid i3bar json: expected '[' but encountered '%c'", + status->buffer[c]); + status_error(status, "[invalid i3bar json]"); return true; } } @@ -155,6 +157,8 @@ bool i3bar_handle_readable(struct status_line *status) { ++buffer_pos; break; } else if (!isspace(status->buffer[buffer_pos])) { + wlr_log(WLR_DEBUG, "Invalid i3bar json: expected ',' but encountered '%c'", + status->buffer[buffer_pos]); status_error(status, "[invalid i3bar json]"); return true; } @@ -166,7 +170,8 @@ bool i3bar_handle_readable(struct status_line *status) { } else { test_object = json_tokener_parse_ex(status->tokener, &status->buffer[buffer_pos], status->buffer_index - buffer_pos); - if (json_tokener_get_error(status->tokener) == json_tokener_success) { + enum json_tokener_error err = json_tokener_get_error(status->tokener); + if (err == json_tokener_success) { if (json_object_get_type(test_object) == json_type_array) { if (last_object) { json_object_put(last_object); @@ -198,7 +203,7 @@ bool i3bar_handle_readable(struct status_line *status) { continue; // look for comma without reading more input } buffer_pos = status->buffer_index = 0; - } else if (json_tokener_get_error(status->tokener) == json_tokener_continue) { + } else if (err == json_tokener_continue) { json_tokener_reset(status->tokener); if (status->buffer_index < status->buffer_size) { // move the object to the start of the buffer @@ -219,6 +224,10 @@ bool i3bar_handle_readable(struct status_line *status) { } } } else { + char last_char = status->buffer[status->buffer_index - 1]; + status->buffer[status->buffer_index - 1] = '\0'; + wlr_log(WLR_DEBUG, "Failed to parse i3bar json - %s: '%s%c'", + json_tokener_error_desc(err), &status->buffer[buffer_pos], last_char); status_error(status, "[failed to parse i3bar json]"); return true; }