dcavalca / rpms / libdnf

Forked from rpms/libdnf 2 years ago
Clone

Blame SOURCES/0001-Fix-some-covscan-warnings.patch

4b9e23
From 153a8ae657a0d2a03f43db076bdadfb4e7dff41e Mon Sep 17 00:00:00 2001
4b9e23
From: Aleš Matěj <amatej@redhat.com>
4b9e23
Date: Mon, 21 Jun 2021 12:20:54 +0200
4b9e23
Subject: [PATCH] Fix some covscan warnings
4b9e23
4b9e23
- missing return value checks
4b9e23
- remove useless/unused code
4b9e23
- set fp to NULL to prevent double free false positive
4b9e23
- checksum function in utils.cpp doesn't have a declaration and
4b9e23
  is used only in utils.cpp -> make it static
4b9e23
4b9e23
Some of these were also compiler warnings
4b9e23
---
4b9e23
 libdnf/dnf-repo.cpp                      | 15 ++++++++++-----
4b9e23
 libdnf/dnf-transaction.cpp               |  2 --
4b9e23
 libdnf/module/ModulePackageContainer.cpp |  2 --
4b9e23
 libdnf/repo/Repo.cpp                     | 28 +++++++++++++++++++++++-----
4b9e23
 libdnf/utils/filesystem.cpp              |  9 ++++++++-
4b9e23
 libdnf/utils/utils.cpp                   |  2 +-
4b9e23
 tests/hawkey/test_iutil.cpp              |  2 ++
4b9e23
 7 files changed, 44 insertions(+), 16 deletions(-)
4b9e23
4b9e23
diff --git a/libdnf/dnf-repo.cpp b/libdnf/dnf-repo.cpp
4b9e23
index 710045f..cc6093e 100644
4b9e23
--- a/libdnf/dnf-repo.cpp
4b9e23
+++ b/libdnf/dnf-repo.cpp
4b9e23
@@ -1966,9 +1966,12 @@ out:
4b9e23
     g_free(updatedata.last_mirror_failure_message);
4b9e23
     g_free(updatedata.last_mirror_url);
4b9e23
     dnf_state_release_locks(state);
4b9e23
-    lr_handle_setopt(priv->repo_handle, NULL, LRO_PROGRESSCB, NULL);
4b9e23
-    lr_handle_setopt(priv->repo_handle, NULL, LRO_HMFCB, NULL);
4b9e23
-    lr_handle_setopt(priv->repo_handle, NULL, LRO_PROGRESSDATA, 0xdeadbeef);
4b9e23
+    if (!lr_handle_setopt(priv->repo_handle, NULL, LRO_PROGRESSCB, NULL))
4b9e23
+            g_debug("Failed to reset LRO_PROGRESSCB to NULL");
4b9e23
+    if (!lr_handle_setopt(priv->repo_handle, NULL, LRO_HMFCB, NULL))
4b9e23
+            g_debug("Failed to reset LRO_HMFCB to NULL");
4b9e23
+    if (!lr_handle_setopt(priv->repo_handle, NULL, LRO_PROGRESSDATA, 0xdeadbeef))
4b9e23
+            g_debug("Failed to set LRO_PROGRESSDATA to 0xdeadbeef");
4b9e23
     return ret;
4b9e23
 } CATCH_TO_GERROR(FALSE)
4b9e23
 
4b9e23
@@ -2296,8 +2299,10 @@ dnf_repo_download_packages(DnfRepo *repo,
4b9e23
 
4b9e23
     ret = TRUE;
4b9e23
 out:
4b9e23
-    lr_handle_setopt(priv->repo_handle, NULL, LRO_PROGRESSCB, NULL);
4b9e23
-    lr_handle_setopt(priv->repo_handle, NULL, LRO_PROGRESSDATA, 0xdeadbeef);
4b9e23
+    if (!lr_handle_setopt(priv->repo_handle, NULL, LRO_PROGRESSCB, NULL))
4b9e23
+            g_debug("Failed to reset LRO_PROGRESSCB to NULL");
4b9e23
+    if (!lr_handle_setopt(priv->repo_handle, NULL, LRO_PROGRESSDATA, 0xdeadbeef))
4b9e23
+            g_debug("Failed to set LRO_PROGRESSDATA to 0xdeadbeef");
4b9e23
     g_free(global_data.last_mirror_failure_message);
4b9e23
     g_free(global_data.last_mirror_url);
4b9e23
     g_slist_free_full(package_targets, (GDestroyNotify)lr_packagetarget_free);
4b9e23
diff --git a/libdnf/dnf-transaction.cpp b/libdnf/dnf-transaction.cpp
4b9e23
index 7c9a3a9..e096658 100644
4b9e23
--- a/libdnf/dnf-transaction.cpp
4b9e23
+++ b/libdnf/dnf-transaction.cpp
4b9e23
@@ -1523,8 +1523,6 @@ dnf_transaction_commit(DnfTransaction *transaction, HyGoal goal, DnfState *state
4b9e23
 
4b9e23
     /* this section done */
4b9e23
     ret = dnf_state_done(state, error);
4b9e23
-    if (!ret)
4b9e23
-        goto out;
4b9e23
 out:
4b9e23
     dnf_transaction_reset(transaction);
4b9e23
     dnf_state_release_locks(state);
4b9e23
diff --git a/libdnf/module/ModulePackageContainer.cpp b/libdnf/module/ModulePackageContainer.cpp
4b9e23
index a20f8df..dc551a9 100644
4b9e23
--- a/libdnf/module/ModulePackageContainer.cpp
4b9e23
+++ b/libdnf/module/ModulePackageContainer.cpp
4b9e23
@@ -1753,8 +1753,6 @@ void ModulePackageContainer::updateFailSafeData()
4b9e23
     if (pImpl->activatedModules) {
4b9e23
         std::vector<ModulePackage *> latest = pImpl->getLatestActiveEnabledModules();
4b9e23
 
4b9e23
-        auto begin = fileNames.begin();
4b9e23
-        auto end = fileNames.end();
4b9e23
         if (g_mkdir_with_parents(pImpl->persistDir.c_str(), 0755) == -1) {
4b9e23
             const char * errTxt = strerror(errno);
4b9e23
             auto logger(Log::getLogger());
4b9e23
diff --git a/libdnf/repo/Repo.cpp b/libdnf/repo/Repo.cpp
4b9e23
index e3a574f..e1d5529 100644
4b9e23
--- a/libdnf/repo/Repo.cpp
4b9e23
+++ b/libdnf/repo/Repo.cpp
4b9e23
@@ -704,7 +704,11 @@ static std::vector<Key> rawkey2infos(int fd) {
4b9e23
 
4b9e23
     // set GPG home dir
4b9e23
     char tmpdir[] = "/tmp/tmpdir.XXXXXX";
4b9e23
-    mkdtemp(tmpdir);
4b9e23
+    if (!mkdtemp(tmpdir)) {
4b9e23
+        const char * errTxt = strerror(errno);
4b9e23
+        throw RepoError(tfm::format(_("Cannot create repo temporary directory \"%s\": %s"),
4b9e23
+                                      tmpdir, errTxt));
4b9e23
+    }
4b9e23
     Finalizer tmpDirRemover([&tmpdir](){
4b9e23
         dnf_remove_recursive(tmpdir, NULL);
4b9e23
     });
4b9e23
@@ -894,8 +898,14 @@ void Repo::Impl::importRepoKeys()
4b9e23
             }
4b9e23
 
4b9e23
             struct stat sb;
4b9e23
-            if (stat(gpgDir.c_str(), &sb) != 0 || !S_ISDIR(sb.st_mode))
4b9e23
-                mkdir(gpgDir.c_str(), 0777);
4b9e23
+            if (stat(gpgDir.c_str(), &sb) != 0 || !S_ISDIR(sb.st_mode)) {
4b9e23
+                int res = mkdir(gpgDir.c_str(), 0777);
4b9e23
+                if (res != 0 && errno != EEXIST) {
4b9e23
+                    auto msg = tfm::format(_("Failed to create directory \"%s\": %d - %s"),
4b9e23
+                                           gpgDir, errno, strerror(errno));
4b9e23
+                    throw RepoError(msg);
4b9e23
+                }
4b9e23
+            }
4b9e23
 
4b9e23
             gpgme_ctx_t ctx;
4b9e23
             gpgme_new(&ctx;;
4b9e23
@@ -1147,7 +1157,11 @@ bool Repo::Impl::isMetalinkInSync()
4b9e23
 {
4b9e23
     auto logger(Log::getLogger());
4b9e23
     char tmpdir[] = "/tmp/tmpdir.XXXXXX";
4b9e23
-    mkdtemp(tmpdir);
4b9e23
+    if (!mkdtemp(tmpdir)) {
4b9e23
+        const char * errTxt = strerror(errno);
4b9e23
+        throw RepoError(tfm::format(_("Cannot create repo temporary directory \"%s\": %s"),
4b9e23
+                                      tmpdir, errTxt));
4b9e23
+    }
4b9e23
     Finalizer tmpDirRemover([&tmpdir](){
4b9e23
         dnf_remove_recursive(tmpdir, NULL);
4b9e23
     });
4b9e23
@@ -1217,7 +1231,11 @@ bool Repo::Impl::isRepomdInSync()
4b9e23
     auto logger(Log::getLogger());
4b9e23
     LrYumRepo *yum_repo;
4b9e23
     char tmpdir[] = "/tmp/tmpdir.XXXXXX";
4b9e23
-    mkdtemp(tmpdir);
4b9e23
+    if (!mkdtemp(tmpdir)) {
4b9e23
+        const char * errTxt = strerror(errno);
4b9e23
+        throw RepoError(tfm::format(_("Cannot create repo temporary directory \"%s\": %s"),
4b9e23
+                                      tmpdir, errTxt));
4b9e23
+    }
4b9e23
     Finalizer tmpDirRemover([&tmpdir](){
4b9e23
         dnf_remove_recursive(tmpdir, NULL);
4b9e23
     });
4b9e23
diff --git a/libdnf/utils/filesystem.cpp b/libdnf/utils/filesystem.cpp
4b9e23
index c3abd3c..89a9478 100644
4b9e23
--- a/libdnf/utils/filesystem.cpp
4b9e23
+++ b/libdnf/utils/filesystem.cpp
4b9e23
@@ -24,6 +24,8 @@
4b9e23
 #include <cerrno>
4b9e23
 #include <cstring>
4b9e23
 
4b9e23
+#include "bgettext/bgettext-lib.h"
4b9e23
+#include "tinyformat/tinyformat.hpp"
4b9e23
 #include "filesystem.hpp"
4b9e23
 #include "../error.hpp"
4b9e23
 
4b9e23
@@ -72,7 +74,12 @@ makeDirPath(std::string filePath)
4b9e23
         previous = position;
4b9e23
         // create directory if necessary
4b9e23
         if (!pathExists(directory.c_str())) {
4b9e23
-            mkdir(directory.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
4b9e23
+            int res = mkdir(directory.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
4b9e23
+            if (res != 0 && errno != EEXIST) {
4b9e23
+                auto msg = tfm::format(_("Failed to create directory \"%s\": %d - %s"),
4b9e23
+                                       directory, errno, strerror(errno));
4b9e23
+                throw Error(msg);
4b9e23
+            }
4b9e23
         }
4b9e23
     }
4b9e23
 }
4b9e23
diff --git a/libdnf/utils/utils.cpp b/libdnf/utils/utils.cpp
4b9e23
index 450718d..15f5275 100644
4b9e23
--- a/libdnf/utils/utils.cpp
4b9e23
+++ b/libdnf/utils/utils.cpp
4b9e23
@@ -301,7 +301,7 @@ void decompress(const char * inPath, const char * outPath, mode_t outMode, const
4b9e23
     fclose(inFile);
4b9e23
 }
4b9e23
 
4b9e23
-void checksum(const char * type, const char * inPath, const char * checksum_valid, bool * valid_out, gchar ** calculated_out)
4b9e23
+static void checksum(const char * type, const char * inPath, const char * checksum_valid, bool * valid_out, gchar ** calculated_out)
4b9e23
 {
4b9e23
     GError * errP{nullptr};
4b9e23
     gboolean valid;
4b9e23
diff --git a/tests/hawkey/test_iutil.cpp b/tests/hawkey/test_iutil.cpp
4b9e23
index 8d00cc9..6dafbdc 100644
4b9e23
--- a/tests/hawkey/test_iutil.cpp
4b9e23
+++ b/tests/hawkey/test_iutil.cpp
4b9e23
@@ -79,11 +79,13 @@ START_TEST(test_checksum)
4b9e23
     /* the taken checksum are not zeros anymore */
4b9e23
     fail_if(checksum_cmp(cs1, cs2) == 0);
4b9e23
     fail_if(checksum_cmp(cs1_sum, cs2_sum) == 0);
4b9e23
+    fp = NULL;
4b9e23
 
4b9e23
     /* append something */
4b9e23
     fail_if((fp = fopen(new_file, "a")) == NULL);
4b9e23
     fail_unless(fwrite("X", 1, 1, fp) == 1);
4b9e23
     fclose(fp);
4b9e23
+    fp = NULL;
4b9e23
 
4b9e23
     /* take the second checksums */
4b9e23
     fail_if((fp = fopen(new_file, "r")) == NULL);
4b9e23
--
4b9e23
libgit2 1.0.1
4b9e23