From 11b372c74155f3bbd7fd37d652b9aa93ab2824b1 Mon Sep 17 00:00:00 2001 From: Dav999-v Date: Mon, 23 Jan 2023 01:56:44 +0100 Subject: [PATCH] Harden next_wrap against getting stuck on a single character If, somehow, a single character is wider than the limit, next_wrap would get you stuck in an infinite loop by refusing to update the start index and giving a line length of 0. Now, it just gives you a line with that single character. I also made some small readability improvements: I renamed next_wrap_s to next_wrap_buf, and added comments at the top of both functions explaining what they do. --- desktop_version/src/Font.cpp | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/desktop_version/src/Font.cpp b/desktop_version/src/Font.cpp index 7fb9a1a6..44370056 100644 --- a/desktop_version/src/Font.cpp +++ b/desktop_version/src/Font.cpp @@ -686,7 +686,9 @@ static bool next_wrap( const char* str, const int maxwidth ) { - /* This function is UTF-8 aware. But start/len still are bytes. */ + /* Get information about the current line in wordwrapped text, + * given this line starts at str[*start]. + * *start is updated to the start of the next line. */ size_t idx = 0; size_t lenfromlastspace = 0; size_t lastspace = 0; @@ -733,6 +735,12 @@ static bool next_wrap( *len = lenfromlastspace; *start = lastspace + 1; } + if (idx == 0) + { + // Oops, we're stuck at a single character + *len = 1; + *start += 1; + } return true; } @@ -743,7 +751,7 @@ next: } } -static bool next_wrap_s( +static bool next_wrap_buf( Font* f, char buffer[], const size_t buffer_size, @@ -751,6 +759,16 @@ static bool next_wrap_s( const char* str, const int maxwidth ) { + /* Get each line of wordwrapped text, writing one line at a time to a buffer. + * Call as follows: + * + * char buf[256]; + * size_t start = 0; + * while (next_wrap_buf(font, buf, sizeof(buf), &start, "String to wordwrap", 320)) + * { + * // buf contains a line of text + * } + */ size_t len = 0; const size_t prev_start = *start; @@ -1177,7 +1195,7 @@ int print_wrap( start = 0; } - while (next_wrap_s(pf.font_sel, buffer, sizeof(buffer), &start, str, maxwidth)) + while (next_wrap_buf(pf.font_sel, buffer, sizeof(buffer), &start, str, maxwidth)) { print(flags, x, y, buffer, r, g, b);