From 53980816dedc1d7403181e950ebe384dc3d4e50b Mon Sep 17 00:00:00 2001 From: Calle Wilund Date: Mon, 10 Jun 2019 14:05:11 +0000 Subject: [PATCH] api.hh: Fix bool parsing in req_param Fixes #4525 req_param uses boost::lexical cast to convert text->var. However, lexical_cast does not handle textual booleans, thus param=true causes not only wrong values, but exceptions. Message-Id: <20190610140511.15478-1-calle@scylladb.com> (cherry picked from commit 26702612f3695af05f2096fe9bdb9cca05d3187d) --- api/api.hh | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/api/api.hh b/api/api.hh index 4eb05e1c91..30d31a63d0 100644 --- a/api/api.hh +++ b/api/api.hh @@ -22,6 +22,7 @@ #pragma once #include +#include #include #include #include @@ -231,7 +232,22 @@ public: return; } try { - value = T{boost::lexical_cast(param)}; + // boost::lexical_cast does not use boolalpha. Converting a + // true/false throws exceptions. We don't want that. + if constexpr (std::is_same_v) { + // Cannot use boolalpha because we (probably) want to + // accept 1 and 0 as well as true and false. And True. And fAlse. + std::transform(param.begin(), param.end(), param.begin(), ::tolower); + if (param == "true" || param == "1") { + value = T(true); + } else if (param == "false" || param == "0") { + value = T(false); + } else { + throw boost::bad_lexical_cast{}; + } + } else { + value = T{boost::lexical_cast(param)}; + } } catch (boost::bad_lexical_cast&) { throw bad_param_exception(format("{} ({}): type error - should be {}", name, param, boost::units::detail::demangle(typeid(Base).name()))); }