Skip to content

Commit d1e4be6

Browse files
authored
Merge pull request NixOS#13227 from NixOS/mergify/bp/2.29-maintenance/pr-13142
libstore: Use `boost::regex` for GC root discovery (backport NixOS#13142)
2 parents 1c618a9 + 29d98da commit d1e4be6

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

packaging/dependencies.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ scope: {
6363
"--with-coroutine"
6464
"--with-iostreams"
6565
];
66+
enableIcu = false;
6667
}).overrideAttrs
6768
(old: {
6869
# Need to remove `--with-*` to use `--with-libraries=...`

src/libstore/gc.cc

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
# include "nix/util/processes.hh"
1414
#endif
1515

16+
#include <boost/regex.hpp>
17+
1618
#include <functional>
1719
#include <queue>
1820
#include <algorithm>
19-
#include <regex>
2021
#include <random>
2122

2223
#include <climits>
@@ -331,8 +332,8 @@ static void readProcLink(const std::filesystem::path & file, UncheckedRoots & ro
331332

332333
static std::string quoteRegexChars(const std::string & raw)
333334
{
334-
static auto specialRegex = std::regex(R"([.^$\\*+?()\[\]{}|])");
335-
return std::regex_replace(raw, specialRegex, R"(\$&)");
335+
static auto specialRegex = boost::regex(R"([.^$\\*+?()\[\]{}|])");
336+
return boost::regex_replace(raw, specialRegex, R"(\$&)");
336337
}
337338

338339
#ifdef __linux__
@@ -354,12 +355,12 @@ void LocalStore::findRuntimeRoots(Roots & roots, bool censor)
354355
auto procDir = AutoCloseDir{opendir("/proc")};
355356
if (procDir) {
356357
struct dirent * ent;
357-
auto digitsRegex = std::regex(R"(^\d+$)");
358-
auto mapRegex = std::regex(R"(^\s*\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+(/\S+)\s*$)");
359-
auto storePathRegex = std::regex(quoteRegexChars(storeDir) + R"(/[0-9a-z]+[0-9a-zA-Z\+\-\._\?=]*)");
358+
static const auto digitsRegex = boost::regex(R"(^\d+$)");
359+
static const auto mapRegex = boost::regex(R"(^\s*\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+(/\S+)\s*$)");
360+
auto storePathRegex = boost::regex(quoteRegexChars(storeDir) + R"(/[0-9a-z]+[0-9a-zA-Z\+\-\._\?=]*)");
360361
while (errno = 0, ent = readdir(procDir.get())) {
361362
checkInterrupt();
362-
if (std::regex_match(ent->d_name, digitsRegex)) {
363+
if (boost::regex_match(ent->d_name, digitsRegex)) {
363364
try {
364365
readProcLink(fmt("/proc/%s/exe" ,ent->d_name), unchecked);
365366
readProcLink(fmt("/proc/%s/cwd", ent->d_name), unchecked);
@@ -386,15 +387,15 @@ void LocalStore::findRuntimeRoots(Roots & roots, bool censor)
386387
std::filesystem::path mapFile = fmt("/proc/%s/maps", ent->d_name);
387388
auto mapLines = tokenizeString<std::vector<std::string>>(readFile(mapFile.string()), "\n");
388389
for (const auto & line : mapLines) {
389-
auto match = std::smatch{};
390-
if (std::regex_match(line, match, mapRegex))
390+
auto match = boost::smatch{};
391+
if (boost::regex_match(line, match, mapRegex))
391392
unchecked[match[1]].emplace(mapFile.string());
392393
}
393394

394395
auto envFile = fmt("/proc/%s/environ", ent->d_name);
395396
auto envString = readFile(envFile);
396-
auto env_end = std::sregex_iterator{};
397-
for (auto i = std::sregex_iterator{envString.begin(), envString.end(), storePathRegex}; i != env_end; ++i)
397+
auto env_end = boost::sregex_iterator{};
398+
for (auto i = boost::sregex_iterator{envString.begin(), envString.end(), storePathRegex}; i != env_end; ++i)
398399
unchecked[i->str()].emplace(envFile);
399400
} catch (SystemError & e) {
400401
if (errno == ENOENT || errno == EACCES || errno == ESRCH)
@@ -413,12 +414,12 @@ void LocalStore::findRuntimeRoots(Roots & roots, bool censor)
413414
// Because of this we disable lsof when running the tests.
414415
if (getEnv("_NIX_TEST_NO_LSOF") != "1") {
415416
try {
416-
std::regex lsofRegex(R"(^n(/.*)$)");
417+
boost::regex lsofRegex(R"(^n(/.*)$)");
417418
auto lsofLines =
418419
tokenizeString<std::vector<std::string>>(runProgram(LSOF, true, { "-n", "-w", "-F", "n" }), "\n");
419420
for (const auto & line : lsofLines) {
420-
std::smatch match;
421-
if (std::regex_match(line, match, lsofRegex))
421+
boost::smatch match;
422+
if (boost::regex_match(line, match, lsofRegex))
422423
unchecked[match[1].str()].emplace("{lsof}");
423424
}
424425
} catch (ExecError & e) {

src/libstore/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ subdir('nix-meson-build-support/libatomic')
9494

9595
boost = dependency(
9696
'boost',
97-
modules : ['container'],
97+
modules : ['container', 'regex'],
9898
include_type: 'system',
9999
)
100100
# boost is a public dependency, but not a pkg-config dependency unfortunately, so we

0 commit comments

Comments
 (0)