Skip to content

Commit

Permalink
Clean up output fd handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
khuey committed Mar 29, 2024
1 parent 1cdfd78 commit a0ecb21
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions src/SourcesCommand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class DebugDirManager {
DebugDirs read_result();

ScopedFd input_pipe_fd;
ScopedFd output_pipe_fd;
FILE* output_file;
pid_t pid;
};

Expand All @@ -178,7 +178,7 @@ DebugDirManager::~DebugDirManager() {
}

input_pipe_fd.close();
output_pipe_fd.close();
fclose(output_file);

int status;
if (waitpid(pid, &status, 0) == -1) {
Expand Down Expand Up @@ -250,7 +250,10 @@ DebugDirManager::DebugDirManager(const string& program, const string& gdb_script

this->pid = pid;
this->input_pipe_fd = ScopedFd(stdin_pipe_fds[1]);
this->output_pipe_fd = ScopedFd(stdout_pipe_fds[0]);
this->output_file = fdopen(stdout_pipe_fds[0], "r");
if (!this->output_file) {
FATAL() << "Failed to fdopen(stdout_pipe_fds[0])";
}
}

DebugDirs DebugDirManager::process_one_binary(const string& binary_path) {
Expand All @@ -276,26 +279,12 @@ DebugDirs DebugDirManager::read_result() {
DebugDirs result;
size_t index;
const char delimiter[2] = ":";
int output_fd;
FILE* output = NULL;

if (!input_pipe_fd.is_open()) {
return result;
}

// Convert our fd to a FILE so we can use fgets instead of writing
// our own buffering code.
output_fd = dup(output_pipe_fd);
if (output_fd < 0) {
FATAL() << "Failed to dup output_pipe_fd";
}

output = fdopen(output_fd, "r");
if (!output) {
FATAL() << "Failed to fdopen(output_fd)";
}

if (!fgets(buf, sizeof(buf) - 1, output)) {
if (!fgets(buf, sizeof(buf) - 1, output_file)) {
FATAL() << "Failed to read gdb script output";
}
index = strcspn(buf, "\n");
Expand All @@ -310,7 +299,7 @@ DebugDirs DebugDirManager::read_result() {
token = strtok(nullptr, delimiter);
}

if (!fgets(buf, sizeof(buf) - 1, output)) {
if (!fgets(buf, sizeof(buf) - 1, output_file)) {
FATAL() << "Failed to read gdb script output";
}
index = strcspn(buf, "\n");
Expand All @@ -330,7 +319,6 @@ DebugDirs DebugDirManager::read_result() {
token = strtok(nullptr, delimiter);
}

fclose(output);
return result;
}

Expand Down

0 comments on commit a0ecb21

Please sign in to comment.