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
This commit is contained in:
Martin Stein 2018-09-07 14:58:34 +02:00 committed by Christian Helmuth
parent 9a2af89c4e
commit cc4a72243d
1 changed files with 7 additions and 5 deletions

View File

@ -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;
});