From 43a7cdd03dafc0fb5a28027c15a9fe20354beadc Mon Sep 17 00:00:00 2001 From: Kristoffer Gryte Date: Sat, 9 May 2026 22:39:51 +0200 Subject: [PATCH] DUNE/Utils/String: fix rightTrimInPlace underflow on empty/all-ws input Empty strings made `r = str - 1` and the loop then read one byte before the buffer; all-whitespace strings walked off the front the same way. Caught by ASan via Parsers/Config::parseFile when getOptionAndValue returned rv==2 (key with no value) and arg was the empty string. Co-Authored-By: Claude Opus 4.7 --- src/DUNE/Utils/String.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/DUNE/Utils/String.cpp b/src/DUNE/Utils/String.cpp index 9851f484ff..5045f137df 100644 --- a/src/DUNE/Utils/String.cpp +++ b/src/DUNE/Utils/String.cpp @@ -82,9 +82,12 @@ namespace DUNE void String::rightTrimInPlace(char* str) { - char* r = str + std::strlen(str) - 1; // Rightmost character + size_t len = std::strlen(str); + if (len == 0) + return; - for (; isspace(*r); --r) + char* r = str + len - 1; // Rightmost character + for (; r >= str && isspace(static_cast(*r)); --r) *r = 0; }