7
7
#include < cstdlib>
8
8
#include < cstdio>
9
9
#include < dlfcn.h>
10
+ #include < optional>
10
11
#include < unistd.h>
11
12
#include < errno.h>
12
13
#include < algorithm>
18
19
#include " Logging.h"
19
20
#include " Utils.h"
20
21
#include < inttypes.h>
22
+ #include " mozilla/ScopeExit.h"
21
23
22
24
// From Utils.h
23
25
mozilla::Atomic<size_t , mozilla::ReleaseAcquire> gPageSize ;
@@ -152,17 +154,17 @@ class DlIteratePhdrHelper {
152
154
DlIteratePhdrHelper () {
153
155
int pipefd[2 ];
154
156
valid_pipe = (pipe (pipefd) == 0 );
155
- read_fd.reset (pipefd[0 ]);
156
- write_fd.reset (pipefd[1 ]);
157
+ read_fd.emplace (pipefd[0 ]);
158
+ write_fd.emplace (pipefd[1 ]);
157
159
}
158
160
159
161
int fill_and_call (dl_phdr_cb callback, const void * l_addr, const char * l_name,
160
162
void * data);
161
163
162
164
private:
163
165
bool valid_pipe;
164
- AutoCloseFD read_fd;
165
- AutoCloseFD write_fd;
166
+ std::optional< AutoCloseFD> read_fd;
167
+ std::optional< AutoCloseFD> write_fd;
166
168
};
167
169
168
170
// This function is called for each shared library iterated over by
@@ -199,7 +201,7 @@ int DlIteratePhdrHelper::fill_and_call(dl_phdr_cb callback, const void* l_addr,
199
201
static_assert (sizeof (raw_ehdr) < PIPE_BUF, " PIPE_BUF is too small" );
200
202
do {
201
203
// writes are atomic when smaller than PIPE_BUF, per POSIX.1-2008.
202
- ret = write (write_fd, l_addr, sizeof (raw_ehdr));
204
+ ret = write (* write_fd, l_addr, sizeof (raw_ehdr));
203
205
} while (ret == -1 && errno == EINTR);
204
206
if (ret != sizeof (raw_ehdr)) {
205
207
if (ret == -1 && errno == EFAULT) {
@@ -212,7 +214,7 @@ int DlIteratePhdrHelper::fill_and_call(dl_phdr_cb callback, const void* l_addr,
212
214
do {
213
215
// Per POSIX.1-2008, interrupted reads can return a length smaller
214
216
// than the given one instead of failing with errno EINTR.
215
- ret = read (read_fd, raw_ehdr + nbytes, sizeof (raw_ehdr) - nbytes);
217
+ ret = read (* read_fd, raw_ehdr + nbytes, sizeof (raw_ehdr) - nbytes);
216
218
if (ret > 0 ) nbytes += ret;
217
219
} while ((nbytes != sizeof (raw_ehdr) && ret > 0 ) ||
218
220
(ret == -1 && errno == EINTR));
@@ -834,7 +836,10 @@ class EnsureWritable {
834
836
/* The interesting part of the /proc/self/maps format looks like:
835
837
* startAddr-endAddr rwxp */
836
838
int result = 0 ;
837
- AutoCloseFILE f (fopen (" /proc/self/maps" , " r" ));
839
+ FILE* const f = fopen (" /proc/self/maps" , " r" );
840
+ const auto cleanup = mozilla::MakeScopeExit ([&]() {
841
+ if (f) fclose (f);
842
+ });
838
843
while (f) {
839
844
unsigned long long startAddr, endAddr;
840
845
char perms[5 ];
0 commit comments