Skip to content

Commit e594de7

Browse files
committedFeb 17, 2021
Use Boost's regex implementation instead of libstdc++'s
The latter runs in unbounded stack space so it crashes on large inputs.
1 parent 6042feb commit e594de7

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed
 

‎flake.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
# Copy libboost_context so we don't get all of Boost in our closure.
136136
# https://github.com/NixOS/nixpkgs/issues/45462
137137
mkdir -p $out/lib
138-
cp -pd ${boost}/lib/{libboost_context*,libboost_thread*,libboost_system*} $out/lib
138+
cp -pd ${boost}/lib/{libboost_context*,libboost_thread*,libboost_system*,libboost_regex*} $out/lib
139139
rm -f $out/lib/*.a
140140
${lib.optionalString stdenv.isLinux ''
141141
chmod u+w $out/lib/*.so.*

‎src/libutil/regex.hh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
#include <boost/regex.hpp>
4+
5+
namespace regex = boost;
6+

‎src/nix/develop.cc

+10-11
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
#include "derivations.hh"
77
#include "affinity.hh"
88
#include "progress-bar.hh"
9-
10-
#include <regex>
9+
#include "regex.hh"
1110

1211
using namespace nix;
1312

@@ -64,42 +63,42 @@ BuildEnvironment readEnvironment(const Path & path)
6463
static std::string indexedArrayRegex =
6564
R"re((?:\(( *\[[0-9]+\]="(?:[^"\\]|\\.)*")*\)))re";
6665

67-
static std::regex declareRegex(
66+
static regex::regex declareRegex(
6867
"^declare -a?x (" + varNameRegex + ")(=(" +
6968
dquotedStringRegex + "|" + indexedArrayRegex + "))?\n");
7069

71-
static std::regex varRegex(
70+
static regex::regex varRegex(
7271
"^(" + varNameRegex + ")=(" + simpleStringRegex + "|" + squotedStringRegex + "|" + indexedArrayRegex + ")\n");
7372

7473
/* Note: we distinguish between an indexed and associative array
7574
using the space before the closing parenthesis. Will
7675
undoubtedly regret this some day. */
77-
static std::regex assocArrayRegex(
76+
static regex::regex assocArrayRegex(
7877
"^(" + varNameRegex + ")=" + R"re((?:\(( *\[[^\]]+\]="(?:[^"\\]|\\.)*")* *\)))re" + "\n");
7978

80-
static std::regex functionRegex(
79+
static regex::regex functionRegex(
8180
"^" + varNameRegex + " \\(\\) *\n");
8281

8382
while (pos != file.end()) {
8483

85-
std::smatch match;
84+
regex::smatch match;
8685

87-
if (std::regex_search(pos, file.cend(), match, declareRegex, std::regex_constants::match_continuous)) {
86+
if (regex::regex_search(pos, file.cend(), match, declareRegex, regex::regex_constants::match_continuous)) {
8887
pos = match[0].second;
8988
exported.insert(match[1]);
9089
}
9190

92-
else if (std::regex_search(pos, file.cend(), match, varRegex, std::regex_constants::match_continuous)) {
91+
else if (regex::regex_search(pos, file.cend(), match, varRegex, regex::regex_constants::match_continuous)) {
9392
pos = match[0].second;
9493
res.env.insert({match[1], Var { .exported = exported.count(match[1]) > 0, .quoted = match[2] }});
9594
}
9695

97-
else if (std::regex_search(pos, file.cend(), match, assocArrayRegex, std::regex_constants::match_continuous)) {
96+
else if (regex::regex_search(pos, file.cend(), match, assocArrayRegex, regex::regex_constants::match_continuous)) {
9897
pos = match[0].second;
9998
res.env.insert({match[1], Var { .associative = true, .quoted = match[2] }});
10099
}
101100

102-
else if (std::regex_search(pos, file.cend(), match, functionRegex, std::regex_constants::match_continuous)) {
101+
else if (regex::regex_search(pos, file.cend(), match, functionRegex, regex::regex_constants::match_continuous)) {
103102
res.bashFunctions = std::string(pos, file.cend());
104103
break;
105104
}

‎src/nix/local.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ nix_CXXFLAGS += -I src/libutil -I src/libstore -I src/libfetchers -I src/libexpr
1818

1919
nix_LIBS = libexpr libmain libfetchers libstore libutil libcmd
2020

21-
nix_LDFLAGS = -pthread $(SODIUM_LIBS) $(EDITLINE_LIBS) $(BOOST_LDFLAGS) -llowdown
21+
nix_LDFLAGS = -pthread $(SODIUM_LIBS) $(EDITLINE_LIBS) $(BOOST_LDFLAGS) -llowdown -lboost_regex
2222

2323
$(foreach name, \
2424
nix-build nix-channel nix-collect-garbage nix-copy-closure nix-daemon nix-env nix-hash nix-instantiate nix-prefetch-url nix-shell nix-store, \

0 commit comments

Comments
 (0)