chantra / rpms / librepo

Forked from rpms/librepo 2 years ago
Clone

Blame SOURCES/0003-Covscan-warnings-and-fail_if-deprication.patch

936122
From fd9d39cf19084dd4244943a3b27ee0a33805f76d Mon Sep 17 00:00:00 2001
936122
From: =?UTF-8?q?Ale=C5=A1=20Mat=C4=9Bj?= <amatej@redhat.com>
936122
Date: Mon, 21 Jun 2021 15:04:51 +0200
936122
Subject: [PATCH 1/2] Fix important covscan warnings
936122
936122
- covscan thinks we are leaking storage from `g_strndup` so be more
936122
explicit that `g_strstrip` is in place.
936122
---
936122
 librepo/downloader.c | 5 ++++-
936122
 1 file changed, 4 insertions(+), 1 deletion(-)
936122
936122
diff --git a/librepo/downloader.c b/librepo/downloader.c
936122
index c5278fbc..f4e8ba2a 100644
936122
--- a/librepo/downloader.c
936122
+++ b/librepo/downloader.c
936122
@@ -494,7 +494,10 @@ lr_headercb(void *ptr, size_t size, size_t nmemb, void *userdata)
936122
         return lr_zckheadercb(ptr, size, nmemb, userdata);
936122
     #endif /* WITH_ZCHUNK */
936122
 
936122
-    char *header = g_strstrip(g_strndup(ptr, size*nmemb));
936122
+    char *header = g_strndup(ptr, size*nmemb);
936122
+    // strips in place
936122
+    g_strstrip(header);
936122
+
936122
     gint64 expected = lrtarget->target->expectedsize;
936122
 
936122
     if (state == LR_HCS_DEFAULT) {
936122
936122
From 5a9e9e99c64e2dc1d181fa14cf348c7f51611a7a Mon Sep 17 00:00:00 2001
936122
From: =?UTF-8?q?Ale=C5=A1=20Mat=C4=9Bj?= <amatej@redhat.com>
936122
Date: Mon, 21 Jun 2021 15:05:13 +0200
936122
Subject: [PATCH 2/2] Fix covscan warnings
936122
936122
- remove unused code
936122
- missing return value checks
936122
- use mkstemp just like other tests
936122
---
936122
 librepo/handle.c        | 12 +++++++++---
936122
 tests/test_downloader.c | 40 ++++++++++++++++++++--------------------
936122
 tests/test_repoconf.c   | 22 +++-------------------
936122
 3 files changed, 32 insertions(+), 42 deletions(-)
936122
936122
diff --git a/librepo/handle.c b/librepo/handle.c
936122
index 8ac7234c..2c23ed09 100644
936122
--- a/librepo/handle.c
936122
+++ b/librepo/handle.c
936122
@@ -628,7 +628,7 @@ lr_handle_setopt(LrHandle *handle,
936122
                         "Value of LRO_LOWSPEEDTIME is too low.");
936122
             ret = FALSE;
936122
         } else {
936122
-            curl_easy_setopt(c_h, CURLOPT_LOW_SPEED_TIME, val_long);
936122
+            c_rc = curl_easy_setopt(c_h, CURLOPT_LOW_SPEED_TIME, val_long);
936122
             handle->lowspeedtime = val_long;
936122
         }
936122
 
936122
@@ -648,7 +648,7 @@ lr_handle_setopt(LrHandle *handle,
936122
                         val_long, handle->maxspeed);
936122
             ret = FALSE;
936122
         } else {
936122
-            curl_easy_setopt(c_h, CURLOPT_LOW_SPEED_LIMIT, val_long);
936122
+            c_rc = curl_easy_setopt(c_h, CURLOPT_LOW_SPEED_LIMIT, val_long);
936122
             handle->lowspeedlimit = val_long;
936122
         }
936122
 
936122
@@ -885,7 +885,13 @@ lr_yum_download_url_retry(int attempts, LrHandle *lr_handle, const char *url,
936122
 
936122
         g_debug("%s: Attempt #%d to download %s failed: %s",
936122
                 __func__, i, url, (*err)->message);
936122
-        ftruncate(fd, 0);
936122
+
936122
+        if (ftruncate(fd, 0) < 0) {
936122
+            g_set_error(err, LR_DOWNLOADER_ERROR, LRE_IO,
936122
+                        "ftruncate() failed: %s", g_strerror(errno));
936122
+            return FALSE;
936122
+        }
936122
+
936122
         g_clear_error (err);
936122
     }
936122
 }
936122
diff --git a/tests/test_downloader.c b/tests/test_downloader.c
936122
index 3a1c60f4..e6155105 100644
936122
--- a/tests/test_downloader.c
936122
+++ b/tests/test_downloader.c
936122
@@ -46,7 +46,7 @@ START_TEST(test_downloader_single_file)
936122
     fail_if(handle == NULL);
936122
 
936122
     char *urls[] = {"http://www.google.com", NULL};
936122
-    lr_handle_setopt(handle, NULL, LRO_URLS, urls);
936122
+    fail_if(!lr_handle_setopt(handle, NULL, LRO_URLS, urls));
936122
     lr_handle_prepare_internal_mirrorlist(handle, FALSE, &tmp_err);
936122
     fail_if(tmp_err);
936122
 
936122
@@ -55,8 +55,7 @@ START_TEST(test_downloader_single_file)
936122
 
936122
     tmpfn1 = lr_pathconcat(test_globals.tmpdir, "single_file_XXXXXX", NULL);
936122
 
936122
-    mktemp(tmpfn1);
936122
-    fd1 = open(tmpfn1, O_RDWR|O_CREAT|O_TRUNC, 0666);
936122
+    fd1 = mkstemp(tmpfn1);
936122
     lr_free(tmpfn1);
936122
     fail_if(fd1 < 0);
936122
 
936122
@@ -86,6 +85,7 @@ START_TEST(test_downloader_single_file)
936122
     }
936122
 
936122
     g_slist_free_full(list, (GDestroyNotify) lr_downloadtarget_free);
936122
+    close(fd1);
936122
 }
936122
 END_TEST
936122
 
936122
@@ -102,8 +102,7 @@ START_TEST(test_downloader_single_file_2)
936122
 
936122
     tmpfn1 = lr_pathconcat(test_globals.tmpdir, "single_file_2_XXXXXX", NULL);
936122
 
936122
-    mktemp(tmpfn1);
936122
-    fd1 = open(tmpfn1, O_RDWR|O_CREAT|O_TRUNC, 0666);
936122
+    fd1 = mkstemp(tmpfn1);
936122
     lr_free(tmpfn1);
936122
     fail_if(fd1 < 0);
936122
 
936122
@@ -131,6 +130,7 @@ START_TEST(test_downloader_single_file_2)
936122
     }
936122
 
936122
     g_slist_free_full(list, (GDestroyNotify) lr_downloadtarget_free);
936122
+    close(fd1);
936122
 }
936122
 END_TEST
936122
 
936122
@@ -151,7 +151,7 @@ START_TEST(test_downloader_two_files)
936122
     fail_if(handle == NULL);
936122
 
936122
     char *urls[] = {"http://www.google.com", NULL};
936122
-    lr_handle_setopt(handle, NULL, LRO_URLS, urls);
936122
+    fail_if(!lr_handle_setopt(handle, NULL, LRO_URLS, urls));
936122
     lr_handle_prepare_internal_mirrorlist(handle, FALSE, &tmp_err);
936122
     fail_if(tmp_err);
936122
 
936122
@@ -160,10 +160,8 @@ START_TEST(test_downloader_two_files)
936122
     tmpfn1 = lr_pathconcat(test_globals.tmpdir, "single_file_1_XXXXXX", NULL);
936122
     tmpfn2 = lr_pathconcat(test_globals.tmpdir, "single_file_2_XXXXXX", NULL);
936122
 
936122
-    mktemp(tmpfn1);
936122
-    mktemp(tmpfn2);
936122
-    fd1 = open(tmpfn1, O_RDWR|O_CREAT|O_TRUNC, 0666);
936122
-    fd2 = open(tmpfn2, O_RDWR|O_CREAT|O_TRUNC, 0666);
936122
+    fd1 = mkstemp(tmpfn1);
936122
+    fd2 = mkstemp(tmpfn2);
936122
     lr_free(tmpfn1);
936122
     lr_free(tmpfn2);
936122
     fail_if(fd1 < 0);
936122
@@ -200,6 +198,8 @@ START_TEST(test_downloader_two_files)
936122
     }
936122
 
936122
     g_slist_free_full(list, (GDestroyNotify) lr_downloadtarget_free);
936122
+    close(fd1);
936122
+    close(fd2);
936122
 }
936122
 END_TEST
936122
 
936122
@@ -220,7 +220,7 @@ START_TEST(test_downloader_three_files_with_error)
936122
     fail_if(handle == NULL);
936122
 
936122
     char *urls[] = {"http://www.google.com", NULL};
936122
-    lr_handle_setopt(handle, NULL, LRO_URLS, urls);
936122
+    fail_if(!lr_handle_setopt(handle, NULL, LRO_URLS, urls));
936122
     lr_handle_prepare_internal_mirrorlist(handle, FALSE, &tmp_err);
936122
     fail_if(tmp_err);
936122
 
936122
@@ -230,12 +230,9 @@ START_TEST(test_downloader_three_files_with_error)
936122
     tmpfn2 = lr_pathconcat(test_globals.tmpdir, "single_file_2_XXXXXX", NULL);
936122
     tmpfn3 = lr_pathconcat(test_globals.tmpdir, "single_file_3_XXXXXX", NULL);
936122
 
936122
-    mktemp(tmpfn1);
936122
-    mktemp(tmpfn2);
936122
-    mktemp(tmpfn3);
936122
-    fd1 = open(tmpfn1, O_RDWR|O_CREAT|O_TRUNC, 0666);
936122
-    fd2 = open(tmpfn2, O_RDWR|O_CREAT|O_TRUNC, 0666);
936122
-    fd3 = open(tmpfn3, O_RDWR|O_CREAT|O_TRUNC, 0666);
936122
+    fd1 = mkstemp(tmpfn1);
936122
+    fd2 = mkstemp(tmpfn2);
936122
+    fd3 = mkstemp(tmpfn3);
936122
     lr_free(tmpfn1);
936122
     lr_free(tmpfn2);
936122
     lr_free(tmpfn3);
936122
@@ -290,6 +287,9 @@ START_TEST(test_downloader_three_files_with_error)
936122
     }
936122
 
936122
     g_slist_free_full(list, (GDestroyNotify) lr_downloadtarget_free);
936122
+    close(fd1);
936122
+    close(fd2);
936122
+    close(fd3);
936122
 }
936122
 END_TEST
936122
 
936122
@@ -331,7 +331,7 @@ START_TEST(test_downloader_checksum)
936122
         fail_if(handle == NULL);
936122
 
936122
         char *urls[] = {"file:///", NULL};
936122
-        lr_handle_setopt(handle, NULL, LRO_URLS, urls);
936122
+        fail_if(!lr_handle_setopt(handle, NULL, LRO_URLS, urls));
936122
         lr_handle_prepare_internal_mirrorlist(handle, FALSE, &tmp_err);
936122
         fail_if(tmp_err);
936122
 
936122
@@ -340,8 +340,7 @@ START_TEST(test_downloader_checksum)
936122
 
936122
         tmpfn1 = lr_pathconcat(test_globals.tmpdir, "single_file_XXXXXX", NULL);
936122
 
936122
-        mktemp(tmpfn1);
936122
-        fd1 = open(tmpfn1, O_RDWR|O_CREAT|O_TRUNC, 0666);
936122
+        fd1 = mkstemp(tmpfn1);
936122
         lr_free(tmpfn1);
936122
         fail_if(fd1 < 0);
936122
 
936122
@@ -382,6 +381,7 @@ START_TEST(test_downloader_checksum)
936122
         }
936122
 
936122
         g_slist_free_full(list, (GDestroyNotify) lr_downloadtarget_free);
936122
+        close(fd1);
936122
     }
936122
 }
936122
 END_TEST
936122
diff --git a/tests/test_repoconf.c b/tests/test_repoconf.c
936122
index 8a56b666..5c85047e 100644
936122
--- a/tests/test_repoconf.c
936122
+++ b/tests/test_repoconf.c
936122
@@ -33,22 +33,6 @@ repoconf_assert_true(LrYumRepoConf *repoconf,
936122
 #define conf_assert_true(option) \
936122
             repoconf_assert_true(conf, (option))
936122
 
936122
-static void
936122
-repoconf_assert_false(LrYumRepoConf *repoconf,
936122
-                      LrYumRepoConfOption option)
936122
-{
936122
-    ck_assert(1);
936122
-    long val = 1L;
936122
-    _cleanup_error_free_ GError *tmp_err = NULL;
936122
-    gboolean ret = lr_yum_repoconf_getinfo(repoconf, &tmp_err, option, &val;;
936122
-    ck_assert(!tmp_err);
936122
-    ck_assert(ret);
936122
-    ck_assert_msg(!val, "Not a 0 (False) value (Option %d)", option);
936122
-}
936122
-
936122
-#define conf_assert_false(option) \
936122
-            repoconf_assert_false(conf, (option))
936122
-
936122
 static void
936122
 repoconf_assert_na(LrYumRepoConf *repoconf,
936122
                    LrYumRepoConfOption option)
936122
@@ -509,7 +493,7 @@ END_TEST
936122
 
936122
 START_TEST(test_write_repoconf)
936122
 {
936122
-    _cleanup_fd_close_ int rc = -1;
936122
+    _cleanup_fd_close_ int fd = -1;
936122
     gboolean ret;
936122
     LrYumRepoConfs *confs;
936122
     LrYumRepoConf * conf;
936122
@@ -519,8 +503,8 @@ START_TEST(test_write_repoconf)
936122
     _cleanup_error_free_ GError *tmp_err = NULL;
936122
 
936122
     // Create a temporary file
936122
-    rc = mkstemp(tmpfn);
936122
-    fail_if(rc == -1);
936122
+    fd = mkstemp(tmpfn);
936122
+    fail_if(fd == -1);
936122
 
936122
     // Create reconfs with one repoconf with one id (one section)
936122
     confs = lr_yum_repoconfs_init();