From ece59d6c6f0b38d605f493bd8bd2e56b1f2adff0 Mon Sep 17 00:00:00 2001 From: Vladimir Prus Date: Thu, 29 Oct 2026 18:31:49 +0000 Subject: [PATCH] Fix recursive error placeholder replacement Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/value_semantic.cpp | 8 +++----- test/exception_test.cpp | 8 +++++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/value_semantic.cpp b/src/value_semantic.cpp index c4feb688e3..80c1c51309 100644 --- a/src/value_semantic.cpp +++ b/src/value_semantic.cpp @@ -272,13 +272,11 @@ namespace boost { namespace program_options { void error_with_option_name::replace_token(const string& from, const string& to) const { - for (;;) + std::size_t pos = 0; + while ((pos = m_message.find(from, pos)) != std::string::npos) { - std::size_t pos = m_message.find(from.c_str(), 0, from.length()); - // not found: all replaced - if (pos == std::string::npos) - return; m_message.replace(pos, from.length(), to); + pos += to.length(); } } diff --git a/test/exception_test.cpp b/test/exception_test.cpp index 438f68db6b..ba9eedc97d 100644 --- a/test/exception_test.cpp +++ b/test/exception_test.cpp @@ -117,6 +117,12 @@ void test_unknown_option() } } +void test_unknown_option_placeholder() +{ + unknown_option err("-%canonical_option%"); + BOOST_CHECK_EQUAL(string(err.what()), "unrecognised option '-%canonical_option%'"); +} + void test_multiple_values() @@ -253,6 +259,7 @@ int main(int /*ac*/, char** /*av*/) test_ambiguous_long(); test_ambiguous_multiple_long_names(); test_unknown_option(); + test_unknown_option_placeholder(); test_multiple_values(); test_multiple_occurrences(); test_multiple_occurrences_with_different_names(); @@ -261,4 +268,3 @@ int main(int /*ac*/, char** /*av*/) return 0; } -