From f6f02eb5ac125272c931cf20da4b28746d2d4a85 Mon Sep 17 00:00:00 2001
From: Sergey Yakubov <sergey.yakubov@desy.de>
Date: Tue, 5 Mar 2019 11:52:56 +0100
Subject: [PATCH] fix mem leaks

---
 common/cpp/src/http_client/curl_http_client.cpp                | 1 -
 common/cpp/src/system_io/system_io_linux.cpp                   | 2 +-
 receiver/src/receiver_statistics.h                             | 1 -
 receiver/src/request_handler_authorize.cpp                     | 2 +-
 receiver/src/requests_dispatcher.cpp                           | 1 +
 receiver/src/statistics.cpp                                    | 2 +-
 receiver/src/statistics.h                                      | 1 +
 receiver/src/statistics_sender_influx_db.h                     | 3 +++
 receiver/unittests/test_request_handler_authorizer.cpp         | 2 +-
 .../producer_receiver/transfer_single_file/check_linux.sh      | 3 +--
 10 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/common/cpp/src/http_client/curl_http_client.cpp b/common/cpp/src/http_client/curl_http_client.cpp
index d104b3f06..6111559f3 100644
--- a/common/cpp/src/http_client/curl_http_client.cpp
+++ b/common/cpp/src/http_client/curl_http_client.cpp
@@ -108,7 +108,6 @@ CurlHttpClient::~CurlHttpClient() {
     if (curl_) {
         curl_easy_cleanup(curl_);
     }
-
 }
 
 
diff --git a/common/cpp/src/system_io/system_io_linux.cpp b/common/cpp/src/system_io/system_io_linux.cpp
index be6393f3f..e2782d35d 100644
--- a/common/cpp/src/system_io/system_io_linux.cpp
+++ b/common/cpp/src/system_io/system_io_linux.cpp
@@ -202,7 +202,7 @@ void SystemIO::CollectFileInformationRecursively(const std::string& path,
 
 void SystemIO::ApplyNetworkOptions(SocketDescriptor socket_fd, Error* err) const {
     //TODO: Need to change network layer code, so everything can be NonBlocking
-    int flag;
+    int flag = 1;
     if (
         /*(flags = fcntl(socket_fd, F_GETFL, 0)) == -1
         ||
diff --git a/receiver/src/receiver_statistics.h b/receiver/src/receiver_statistics.h
index 83be94197..877eb5453 100644
--- a/receiver/src/receiver_statistics.h
+++ b/receiver/src/receiver_statistics.h
@@ -10,7 +10,6 @@ enum StatisticEntity : int {
     kDatabase = 0,
     kDisk,
     kNetwork,
-    kAuthorizer
 };
 
 static const std::vector<std::string> kStatisticEntityNames = {"db_share", "disk_share", "network_share"};
diff --git a/receiver/src/request_handler_authorize.cpp b/receiver/src/request_handler_authorize.cpp
index b7ff5d211..d0662d6b8 100644
--- a/receiver/src/request_handler_authorize.cpp
+++ b/receiver/src/request_handler_authorize.cpp
@@ -100,7 +100,7 @@ RequestHandlerAuthorize::RequestHandlerAuthorize(): log__{GetDefaultReceiverLogg
 }
 
 StatisticEntity RequestHandlerAuthorize::GetStatisticEntity() const {
-    return StatisticEntity::kAuthorizer;
+    return StatisticEntity::kNetwork;
 }
 
 
diff --git a/receiver/src/requests_dispatcher.cpp b/receiver/src/requests_dispatcher.cpp
index 8186be412..c50319f84 100644
--- a/receiver/src/requests_dispatcher.cpp
+++ b/receiver/src/requests_dispatcher.cpp
@@ -32,6 +32,7 @@ Error RequestsDispatcher::ProcessRequest(const std::unique_ptr<Request>& request
     Error handle_err;
     handle_err = request->Handle(statistics__);
     GenericNetworkResponse generic_response;
+    generic_response.op_code = request->GetOpCode();
     generic_response.error_code = GetNetworkCodeFromError(handle_err);
     strcpy(generic_response.message, "");
     if (handle_err) {
diff --git a/receiver/src/statistics.cpp b/receiver/src/statistics.cpp
index d35773cf1..eac194d04 100644
--- a/receiver/src/statistics.cpp
+++ b/receiver/src/statistics.cpp
@@ -55,7 +55,7 @@ void Statistics::IncreaseRequestCounter() noexcept {
 
 Statistics::Statistics(unsigned int write_frequency) :
     write_interval_{write_frequency} {
-    statistics_sender_list__.emplace_back(new StatisticsSenderInfluxDb);
+    statistics_sender_list__.emplace_back(std::unique_ptr<StatisticsSender> {new StatisticsSenderInfluxDb});
 //    statistics_sender_list__.emplace_back(new StatisticsSenderFluentd);
     ResetStatistics();
 }
diff --git a/receiver/src/statistics.h b/receiver/src/statistics.h
index 791d296a1..05a2c6878 100644
--- a/receiver/src/statistics.h
+++ b/receiver/src/statistics.h
@@ -33,6 +33,7 @@ class Statistics {
     VIRTUAL void AddTag(const std::string& name, const std::string& value) noexcept;
     void SetWriteInterval(uint64_t interval_ms);
     std::vector<std::unique_ptr<StatisticsSender>> statistics_sender_list__;
+    virtual ~Statistics() = default;
   protected:
     static const unsigned int kDefaultStatisticWriteIntervalMs = 10000;
     virtual StatisticsToSend PrepareStatisticsToSend() const noexcept;
diff --git a/receiver/src/statistics_sender_influx_db.h b/receiver/src/statistics_sender_influx_db.h
index 909dadd56..65c1a579d 100644
--- a/receiver/src/statistics_sender_influx_db.h
+++ b/receiver/src/statistics_sender_influx_db.h
@@ -13,6 +13,9 @@ class StatisticsSenderInfluxDb : public StatisticsSender {
     virtual void SendStatistics(const StatisticsToSend& statistic) const noexcept override;
     std::unique_ptr<HttpClient> httpclient__;
     const AbstractLogger* log__;
+    ~StatisticsSenderInfluxDb() {
+        printf("Deleting StatisticsSenderInfluxDb\n");
+    };
   private:
     std::string StatisticsToString(const StatisticsToSend& statistic) const noexcept;
 
diff --git a/receiver/unittests/test_request_handler_authorizer.cpp b/receiver/unittests/test_request_handler_authorizer.cpp
index 75ada9ccd..8e179fa71 100644
--- a/receiver/unittests/test_request_handler_authorizer.cpp
+++ b/receiver/unittests/test_request_handler_authorizer.cpp
@@ -148,7 +148,7 @@ class AuthorizerHandlerTests : public Test {
 
 TEST_F(AuthorizerHandlerTests, CheckStatisticEntity) {
     auto entity = handler.GetStatisticEntity();
-    ASSERT_THAT(entity, Eq(asapo::StatisticEntity::kAuthorizer));
+    ASSERT_THAT(entity, Eq(asapo::StatisticEntity::kNetwork));
 }
 
 TEST_F(AuthorizerHandlerTests, ErrorNotAuthorizedYet) {
diff --git a/tests/automatic/producer_receiver/transfer_single_file/check_linux.sh b/tests/automatic/producer_receiver/transfer_single_file/check_linux.sh
index cb98e5067..a24e10a64 100644
--- a/tests/automatic/producer_receiver/transfer_single_file/check_linux.sh
+++ b/tests/automatic/producer_receiver/transfer_single_file/check_linux.sh
@@ -36,5 +36,4 @@ $1 localhost:8400 ${beamtime_id} 100 1 1  0 30
 
 ls -ln ${receiver_folder}/1 | awk '{ print $5 }'| grep 100000
 
-
-$1 localhost:8400 wrong_beamtime_id 100 1 1 0 1 2>1 | grep "authorization failed"
+$1 localhost:8400 wrong_beamtime_id 100 1 1 0 1 2>&1 | tee /dev/stderr | grep "authorization failed"
-- 
GitLab