Skip to content

Commit

Permalink
print_lines: fix bugs in line length calculation
Browse files Browse the repository at this point in the history
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 genodelabs#2967
  • Loading branch information
m-stein committed Sep 7, 2018
1 parent eefeb4e commit dc32bf4
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions repos/base/include/util/print_lines.h
Expand Up @@ -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;
});

Expand Down

0 comments on commit dc32bf4

Please sign in to comment.