From 116a54e98038b899c0e1d015183e4f8fbc234aef Mon Sep 17 00:00:00 2001 From: Ravin Perera <33562092+ravinsp@users.noreply.github.com> Date: Mon, 26 Oct 2020 08:11:55 +0530 Subject: [PATCH] Fixed ledger read fd leak. Reordered subsystem init. (#138) --- src/ledger.cpp | 3 +++ src/main.cpp | 22 +++++++++++----------- src/sc.cpp | 1 + 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/ledger.cpp b/src/ledger.cpp index 6d28e9bc..83ce28c5 100644 --- a/src/ledger.cpp +++ b/src/ledger.cpp @@ -333,6 +333,7 @@ namespace ledger struct stat st; if (fstat(fd, &st) == -1) { + close(fd); LOG_ERROR << errno << ": Error in ledger file stat. " << file_path; return -1; } @@ -340,10 +341,12 @@ namespace ledger buffer.resize(st.st_size); if (read(fd, buffer.data(), buffer.size()) == -1) { + close(fd); LOG_ERROR << errno << ": Error reading ledger file. " << file_path; return -1; } + close(fd); return 0; } diff --git a/src/main.cpp b/src/main.cpp index 9c3f953f..dd98c0bf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,14 +68,14 @@ int parse_cmd(int argc, char **argv) */ void deinit() { - consensus::deinit(); - ledger::deinit(); - state_sync::deinit(); - state_serve::deinit(); - read_req::deinit(); - sc::deinit(); usr::deinit(); p2p::deinit(); + read_req::deinit(); + consensus::deinit(); + state_sync::deinit(); + state_serve::deinit(); + sc::deinit(); + ledger::deinit(); } void sigint_handler(int signum) @@ -192,14 +192,14 @@ int main(int argc, char **argv) << (conf::cfg.startup_mode == conf::OPERATING_MODE::OBSERVER ? "Observer" : "Proposer"); LOG_INFO << "Public key: " << conf::cfg.pubkeyhex.substr(2); // Public key without 'ed' prefix. - if (p2p::init() != 0 || - usr::init() != 0 || + if (ledger::init() || sc::init() || - read_req::init() != 0 || state_serve::init() != 0 || state_sync::init() != 0 || - ledger::init() || - consensus::init() != 0) + consensus::init() != 0 || + read_req::init() != 0 || + p2p::init() != 0 || + usr::init() != 0) { deinit(); return -1; diff --git a/src/sc.cpp b/src/sc.cpp index 7a74f2a7..776a7aef 100644 --- a/src/sc.cpp +++ b/src/sc.cpp @@ -310,6 +310,7 @@ namespace sc // Write the json message and close write fd. if (write(stdinpipe[1], json.data(), json.size()) == -1) { + close(stdinpipe[1]); LOG_ERROR << errno << ": Failed to write to stdin of contract process."; return -1; }