string-util: Prevent infinite loop pegging CPU on malformed ESC input

string_has_ansi_sequence() currently does this to look for ESC input:

    t = memchr(s, 0x1B, ...)

So each iteration re-searches from the original start pointer. But if we
find an ESC byte that does *not* start a valid ANSI sequence (like "\x1B
", or an ESC at the end of the string), then ansi_sequence_length()
returns 0, and if that ESC is still in the search window, we will just
spin consuming 100% CPU forever.

Fix this by always advancing past rejected ESC bytes.
This commit is contained in:
Chris Down
2026-02-14 21:49:24 +01:00
committed by Mike Yuan
parent 8734b0ff32
commit 0a66431031
2 changed files with 13 additions and 1 deletions
+3 -1
View File
@@ -267,9 +267,11 @@ static size_t ansi_sequence_length(const char *s, size_t len) {
static bool string_has_ansi_sequence(const char *s, size_t len) {
const char *t = s;
while ((t = memchr(s, 0x1B, len - (t - s))))
while ((t = memchr(t, 0x1B, len - (t - s)))) {
if (ansi_sequence_length(t, len - (t - s)) > 0)
return true;
t++;
}
return false;
}