From b6cf62d00a74274b646b961bdbaf2b62872d4d21 Mon Sep 17 00:00:00 2001 From: ronak69 Date: Wed, 27 Mar 2024 17:54:56 +0000 Subject: [PATCH] Shell: Correctly auto format command lines consisting of only whitespace When the command line consists of only whitespace characters, the auto formatter basically tries to trim the trailing whitespace. But due to a subtle bug of decrementing an iterator (which is an unsigned integer) past 0 (or the start of the buffer), that operation resulted in an index out of bounds error and a crash. --- Userland/Shell/Formatter.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Userland/Shell/Formatter.h b/Userland/Shell/Formatter.h index da3117d0784..80bda86606b 100644 --- a/Userland/Shell/Formatter.h +++ b/Userland/Shell/Formatter.h @@ -30,8 +30,11 @@ public: return; size_t offset = 0; - for (auto ptr = m_source.end() - 1; ptr >= m_source.begin() && isspace(*ptr); --ptr) + for (auto ptr = m_source.end() - 1; isspace(*ptr); --ptr) { ++offset; + if (ptr == m_source.begin()) + break; + } m_trivia = m_source.substring_view(m_source.length() - offset, offset); }