From e199acca0562c32b13706ed7bc3f5047fd5fb318 Mon Sep 17 00:00:00 2001 From: Martin Stein Date: Tue, 18 Jun 2019 20:57:44 +0200 Subject: [PATCH] util/print_lines: don't skip end of long lines Print the end of oversized lines (> 200 chars) to new lines instead of skipping it Fixes #3422 --- repos/base/include/util/print_lines.h | 37 +++++++++++++++------------ 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/repos/base/include/util/print_lines.h b/repos/base/include/util/print_lines.h index a8b228bba1..862c37993e 100644 --- a/repos/base/include/util/print_lines.h +++ b/repos/base/include/util/print_lines.h @@ -66,30 +66,33 @@ void Genode::print_lines(char const *string, size_t len, FUNC const &func) if (Genode::strcmp(first_line, string, num_indent_chars) == 0) string += num_indent_chars; - size_t const line_len = - ({ - size_t i = 0; - for (; i < len; i++) { - if (string[i] == '\0' || string[i] == '\n') { + size_t line_len = 0; + size_t skip_char = 1; - /* the line length is the offset of the last real character + 1 */ - i++; - break; - } - } - i; - }); + for (; line_len < len; line_len++) { + if (string[line_len] == '\0' || string[line_len] == '\n') { + line_len++; + break; + } + if (line_len == MAX_LINE_LEN) { + skip_char = 0; + break; + } + } if (!line_len) break; - /* - * Copy line from (untrusted) caller to local line buffer - */ - char line_buf[MAX_LINE_LEN]; - Genode::strncpy(line_buf, string, Genode::min(line_len, sizeof(line_buf))); + /* buffer for sub-string of the input string plus null-termination */ + char line_buf[MAX_LINE_LEN + 1]; + + /* give strncpy one more as it will add the null termination */ + Genode::strncpy(line_buf, string, line_len - skip_char + 1); + + /* process null-terminated string in buffer */ func(line_buf); + /* move forward to the next sub-string to process */ string += line_len; len -= line_len; }