Skip to content

Commit 1c4bbbe

Browse files
fix: Handle PATH-based invocation of scip-clang (#151)
1 parent 2c2c99d commit 1c4bbbe

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

indexer/Driver.cc

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "boost/interprocess/ipc/message_queue.hpp"
2323
#include "boost/process/child.hpp"
2424
#include "boost/process/io.hpp"
25+
#include "boost/process/search_path.hpp"
2526
#include "rapidjson/document.h"
2627
#include "rapidjson/error/en.h"
2728
#include "rapidjson/filereadstream.h"
@@ -111,7 +112,7 @@ struct WorkerInfo {
111112
};
112113

113114
struct DriverOptions {
114-
std::string workerExecutablePath;
115+
AbsolutePath workerExecutablePath;
115116
RootPath projectRootPath;
116117
AbsolutePath compdbPath;
117118
AbsolutePath indexOutputPath;
@@ -129,7 +130,7 @@ struct DriverOptions {
129130
std::vector<std::string> originalArgv;
130131

131132
explicit DriverOptions(std::string driverId, const CliOptions &cliOpts)
132-
: workerExecutablePath(cliOpts.scipClangExecutablePath),
133+
: workerExecutablePath(),
133134
projectRootPath(AbsolutePath("/"), RootKind::Project), compdbPath(),
134135
indexOutputPath(), numWorkers(cliOpts.numWorkers),
135136
receiveTimeout(cliOpts.receiveTimeout),
@@ -158,6 +159,28 @@ struct DriverOptions {
158159
RootRelativePathRef(path, RootKind::Project));
159160
};
160161

162+
// Strictly speaking, there is a TOCTOU problem here, as scip-clang
163+
// can go missing between this check and the actual execve invocation
164+
// when spawning a worker, but it's simpler to check this here and
165+
// provide a nicer error message.
166+
if (cliOpts.scipClangExecutablePath.find(
167+
std::filesystem::path::preferred_separator)
168+
== std::string::npos) {
169+
auto newPath =
170+
boost::process::search_path(cliOpts.scipClangExecutablePath);
171+
if (newPath.empty()) {
172+
spdlog::error("scip-clang needs to be able to re-invoke itself,"
173+
" but couldn't find scip-clang on PATH."
174+
" Hint: Use a modified PATH to invoke scip-clang,"
175+
" or invoke scip-clang using an absolute path");
176+
std::exit(1);
177+
}
178+
this->workerExecutablePath = AbsolutePath(std::string(newPath.native()));
179+
} else {
180+
setAbsolutePath(cliOpts.scipClangExecutablePath,
181+
this->workerExecutablePath);
182+
}
183+
161184
setAbsolutePath(cliOpts.indexOutputPath, this->indexOutputPath);
162185
setAbsolutePath(cliOpts.compdbPath, this->compdbPath);
163186

@@ -754,7 +777,7 @@ class Driver {
754777

755778
boost::process::child spawnWorker(WorkerId workerId) {
756779
std::vector<std::string> args;
757-
args.push_back(this->options.workerExecutablePath);
780+
args.push_back(this->options.workerExecutablePath.asStringRef());
758781
args.push_back("--worker-mode=ipc");
759782
args.push_back(fmt::format("--driver-id={}", this->id));
760783
args.push_back(fmt::format("--worker-id={}", workerId));

0 commit comments

Comments
 (0)