From a64964a2219bdfe5906b9de3838d0b97e1b1edb5 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Wed, 24 Feb 2021 21:16:48 +0300 Subject: use SafeInt to make integers a bit more safe --- src/cmd_line.cpp | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/cmd_line.cpp b/src/cmd_line.cpp index d3dc781..238e0bc 100644 --- a/src/cmd_line.cpp +++ b/src/cmd_line.cpp @@ -8,6 +8,8 @@ #include #include +#include + #include #include @@ -96,40 +98,35 @@ CommandLine::CommandLine(std::vector&& argv) : m_args(std::move(arg } std::string CommandLine::escape(const std::string& arg) { - std::string safe; - // Including the quotes: - safe.reserve(arg.length() + 2); - - safe.push_back('"'); + std::ostringstream safe; + safe << '"'; for (auto it = arg.cbegin(); it != arg.cend(); ++it) { - std::size_t numof_backslashes = 0; + SafeInt numof_backslashes{0}; for (; it != arg.cend() && *it == '\\'; ++it) ++numof_backslashes; if (it == arg.cend()) { - safe.reserve(safe.capacity() + numof_backslashes); - safe.append(2 * numof_backslashes, '\\'); + safe << std::string(2 * numof_backslashes, '\\'); break; } switch (*it) { case L'"': - safe.reserve(safe.capacity() + numof_backslashes + 1); - safe.append(2 * numof_backslashes + 1, '\\'); + safe << std::string(2 * numof_backslashes + 1, '\\'); break; default: - safe.append(numof_backslashes, '\\'); + safe << std::string(numof_backslashes, '\\'); break; } - safe.push_back(*it); + safe << *it; } - safe.push_back('"'); - return safe; + safe << '"'; + return safe.str(); } std::string CommandLine::escape_cmd(const std::string& arg) { -- cgit v1.2.3