Skip to content

Commit 16ebae8

Browse files
committed
Handle fork(2) failures
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.
1 parent fe76b28 commit 16ebae8

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
@@ -4,6 +4,7 @@
44

55
#include <mp/util.h>
66

7+
#include <errno.h>
78
#include <kj/array.h>
89
#include <pthread.h>
910
#include <sstream>
@@ -13,6 +14,7 @@
1314
#include <sys/types.h>
1415
#include <sys/un.h>
1516
#include <sys/wait.h>
17+
#include <system_error>
1618
#include <unistd.h>
1719

1820
#if __linux__
@@ -83,6 +85,9 @@ int SpawnProcess(int& pid, FdToArgsFn&& fd_to_args)
8385
}
8486

8587
pid = fork();
88+
if (pid == -1) {
89+
throw std::system_error(errno, std::system_category(), "fork");
90+
}
8691
if (close(fds[pid ? 0 : 1]) != 0) {
8792
throw std::system_error(errno, std::system_category());
8893
}

0 commit comments

Comments
 (0)