From 5bfc150688ea748595b6535eb1dc5d777baf16e4 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 12 Feb 2021 17:13:57 +0100 Subject: 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(). --- apt-pkg/algorithms.cc | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'apt-pkg/algorithms.cc') 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 #include #include -#include #include #include #include @@ -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) << "$"; } } } -- cgit v1.2.3-70-g09d2