aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2021-02-24 21:16:48 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2021-02-24 21:48:54 +0300
commita64964a2219bdfe5906b9de3838d0b97e1b1edb5 (patch)
treec2ddeeeecfda89eb6883dcef494a141c65a095f3 /src
parentadd README.md (diff)
downloadwinapi-common-a64964a2219bdfe5906b9de3838d0b97e1b1edb5.tar.gz
winapi-common-a64964a2219bdfe5906b9de3838d0b97e1b1edb5.zip
use SafeInt to make integers a bit more safe
Diffstat (limited to 'src')
-rw-r--r--src/cmd_line.cpp25
1 files changed, 11 insertions, 14 deletions
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 <winapi/utf8.hpp>
#include <winapi/utils.hpp>
+#include <SafeInt.hpp>
+
#include <boost/algorithm/string.hpp>
#include <boost/config.hpp>
@@ -96,40 +98,35 @@ CommandLine::CommandLine(std::vector<std::string>&& 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<std::size_t> 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) {