Skip to content

Commit 49a9637

Browse files
committed
Merge #22: Handle fork(2) failures
16ebae8 Handle fork(2) failures (Vasil Dimov) Pull request description: Generate() and SpawnProcess() call fork(2) but did not check if it failed (return -1) and continued execution with possible unexpected results. Handle fork(2) failure by throwing an exception. ACKs for top commit: ryanofsky: Tested ACK 16ebae8. Thanks for catching this, and also fixing more missing includes Tree-SHA512: 72c9a6842772207e8617b7bfa69c2be3cde4888d1225391ee287510f0c5558ba5099369080a2dc638c2486161f21720f15f49477f897da5578ad8461a663489b
2 parents 50b6a7f + 16ebae8 commit 49a9637

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/mp/gen.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
#include <algorithm>
99
#include <capnp/schema-parser.h>
10+
#include <errno.h>
1011
#include <fstream>
1112
#include <map>
1213
#include <set>
1314
#include <sstream>
15+
#include <system_error>
1416
#include <unistd.h>
1517
#include <vector>
1618

@@ -137,6 +139,9 @@ void Generate(kj::StringPtr src_prefix,
137139
args.emplace_back("--output=" capnp_PREFIX "/bin/capnpc-c++");
138140
args.emplace_back(src_file);
139141
int pid = fork();
142+
if (pid == -1) {
143+
throw std::system_error(errno, std::system_category(), "fork");
144+
}
140145
if (!pid) {
141146
mp::ExecProcess(args);
142147
}

src/mp/util.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <mp/config.h>
66
#include <mp/util.h>
77

8+
#include <errno.h>
89
#include <kj/array.h>
910
#include <pthread.h>
1011
#include <sstream>
@@ -14,6 +15,7 @@
1415
#include <sys/types.h>
1516
#include <sys/un.h>
1617
#include <sys/wait.h>
18+
#include <system_error>
1719
#include <thread>
1820
#include <unistd.h>
1921

@@ -101,6 +103,9 @@ int SpawnProcess(int& pid, FdToArgsFn&& fd_to_args)
101103
}
102104

103105
pid = fork();
106+
if (pid == -1) {
107+
throw std::system_error(errno, std::system_category(), "fork");
108+
}
104109
if (close(fds[pid ? 0 : 1]) != 0) {
105110
throw std::system_error(errno, std::system_category());
106111
}

0 commit comments

Comments
 (0)