From cc4a72243db461159573db6c1ef6da335b153ebf Mon Sep 17 00:00:00 2001 From: Martin Stein Date: Fri, 7 Sep 2018 14:58:34 +0200 Subject: [PATCH] print_lines: fix bugs in line length calculation 1) The loop for determining the line length read from a character offset before checking whether the offset is smaller than the given string length. This could have caused access outside the string buffer. 2) The routine for determining the line length first seeked for the offset of the last real character of the line and than added one for getting the length but only if the following character was '\n'. This has to be done for any other line-terminating character too. The only case where you don't want to do this is when the end of the whole string is reached. Issue #2967 --- repos/base/include/util/print_lines.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/repos/base/include/util/print_lines.h b/repos/base/include/util/print_lines.h index 050510703..a8b228bba 100644 --- a/repos/base/include/util/print_lines.h +++ b/repos/base/include/util/print_lines.h @@ -69,12 +69,14 @@ void Genode::print_lines(char const *string, size_t len, FUNC const &func) size_t const line_len = ({ size_t i = 0; - for (; string[i] && i < len && string[i] != '\n'; i++); - - /* the newline character belongs to the line */ - if (string[i] == '\n') - i++; + for (; i < len; i++) { + if (string[i] == '\0' || string[i] == '\n') { + /* the line length is the offset of the last real character + 1 */ + i++; + break; + } + } i; });