diff options
author | Julian Andres Klode <julian.klode@canonical.com> | 2021-02-12 17:13:57 +0100 |
---|---|---|
committer | Julian Andres Klode <julian.klode@canonical.com> | 2021-02-12 17:13:57 +0100 |
commit | 5bfc150688ea748595b6535eb1dc5d777baf16e4 (patch) | |
tree | d2354c678cb0e2b4ebf4383c72a46cca6563891a /apt-pkg | |
parent | 3b198616423daaef69c938fbcc5dd11a1e8f866c (diff) |
kernels: Avoid std::regex for escaping '.' and '+'
std::regex pulls in about 50 weak symbols which is complete and
utter madness, especially because we version all our symbols, so no
other library could ever reuse them.
Avoid using the regular expression here all together, loop using
string::find_first_of() and insert backslashes with strng::insert().
Diffstat (limited to 'apt-pkg')
-rw-r--r-- | apt-pkg/algorithms.cc | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/apt-pkg/algorithms.cc b/apt-pkg/algorithms.cc index 4c267c91c..fb0b7dca7 100644 --- a/apt-pkg/algorithms.cc +++ b/apt-pkg/algorithms.cc @@ -34,7 +34,6 @@ #include <cstdlib> #include <iostream> #include <map> -#include <regex> #include <set> #include <sstream> #include <string> @@ -1586,22 +1585,27 @@ std::string GetProtectedKernelsRegex(pkgCache *cache, bool ReturnRemove) std::clog << "Keeping previous kernel " << previous->first << std::endl; keep.insert(previous->first); } - - std::regex special("([\\.\\+])"); + // Escape special characters '.' and '+' in version strings so we can build a regular expression + auto escapeSpecial = [](std::string input) -> std::string { + for (size_t pos = 0; (pos = input.find_first_of(".+", pos)) != input.npos; pos += 2) { + input.insert(pos, 1, '\\'); + } + return input; + }; std::ostringstream ss; for (auto &pattern : _config->FindVector("APT::VersionedKernelPackages")) { // Legacy compatibility: Always protected the booted uname and last installed uname if (not lastInstalledUname.empty()) - ss << "|^" << pattern << "-" << std::regex_replace(lastInstalledUname, special, "\\$1") << "$"; + ss << "|^" << pattern << "-" << escapeSpecial(lastInstalledUname) << "$"; if (*uts.release) - ss << "|^" << pattern << "-" << std::regex_replace(uts.release, special, "\\$1") << "$"; + ss << "|^" << pattern << "-" << escapeSpecial(uts.release) << "$"; for (auto const &kernel : version2unames) { if (ReturnRemove ? keep.find(kernel.first) == keep.end() : keep.find(kernel.first) != keep.end()) { for (auto const &uname : kernel.second) - ss << "|^" << pattern << "-" << std::regex_replace(uname, special, "\\$1") << "$"; + ss << "|^" << pattern << "-" << escapeSpecial(uname) << "$"; } } } |