From 5010ca2c9971cd314237aea5959e32297e7b5cb5 Mon Sep 17 00:00:00 2001 From: James Prestwood Date: Wed, 12 Oct 2022 11:35:19 -0700 Subject: [PATCH] client: validate utf-8 before displaying row In theory any input to this function should have valid utf-8 but just in case the strings should be validated. This removes the need to check the return of l_utf8_get_codepoint which is useful since there is no graceful failure path at this point. --- client/display.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/client/display.c b/client/display.c index ddd9551c..03ea33a1 100644 --- a/client/display.c +++ b/client/display.c @@ -420,11 +420,7 @@ static char* next_line(char *s, unsigned int *max, char **color_out) last_space = i; sequence_len = l_utf8_get_codepoint(&s[i], s_len - i, &w); - if (sequence_len < 0) { - sequence_len = 1; - sequence_columns = 1; - } else - sequence_columns = wcwidth(w); + sequence_columns = wcwidth(w); } /* Compensate max bytes */ @@ -532,9 +528,17 @@ void display_table_row(const char *margin, unsigned int ncolumns, ...) for (i = 0; i < ncolumns; i++) { struct table_entry *e = &entries[i]; + char *v; e->width = va_arg(va, unsigned int); - e->next = l_strdup(va_arg(va, char*)); + v = va_arg(va, char *); + + if (!l_utf8_validate(v, strlen(v), NULL)) { + display_error("Invalid utf-8 string!"); + goto done; + } + + e->next = l_strdup(v); str += entry_append(e, str); } @@ -565,9 +569,13 @@ void display_table_row(const char *margin, unsigned int ncolumns, ...) str = buf; } +done: for (i = 0; i < ncolumns; i++) { if (entries[i].color) l_free(entries[i].color); + + if (entries[i].next) + l_free(entries[i].next); } }