diff options
author | Aleš Matěj <amatej@redhat.com> | 2020-11-06 11:47:23 +0300 |
---|---|---|
committer | Neal Gompa (ニール・ゴンパ) <ngompa13@gmail.com> | 2021-01-06 16:58:12 +0300 |
commit | 54bba334e26e5456cbf91687d1429f4b00bf09a2 (patch) | |
tree | 43d484c933e46ab8b9b48d6cf4ad29fc7f50b994 | |
parent | 59b508f872d5200bc6b04c32de1793055ec07e4f (diff) |
Revert back to old API of cr_compress_file_with_stat and cr_compress_file
In commit https://github.com/rpm-software-management/createrepo_c/commit/79975df0864bd471a199f37914cdda39da281936
`in_dst` argument was changed to actually return allocated string with
the final destination if the input `in_dst` was NULL, a direcotry or had
a different compression suffix. However the value is actually never used
anywhere and it is causing a memory leak.
This commit reverts to the previous version.
https://github.com/rpm-software-management/createrepo_c/issues/232
= changelog =
msg: Revert back to old c API for destination file of cr_compress_file_with_stat and cr_compress_file to prevent a memory leak
type: bugfix
resolves: https://github.com/rpm-software-management/createrepo_c/issues/232
-rw-r--r-- | src/misc.c | 25 | ||||
-rw-r--r-- | src/misc.h | 4 | ||||
-rw-r--r-- | src/modifyrepo_shared.c | 2 | ||||
-rw-r--r-- | src/python/misc-py.c | 2 | ||||
-rw-r--r-- | src/threads.c | 2 | ||||
-rw-r--r-- | tests/test_misc.c | 48 |
6 files changed, 46 insertions, 37 deletions
@@ -446,7 +446,7 @@ cr_copy_file(const char *src, const char *in_dst, GError **err) int cr_compress_file_with_stat(const char *src, - char **in_dst, + const char *in_dst, cr_CompressionType compression, cr_ContentStat *stat, const char *zck_dict_dir, @@ -458,7 +458,7 @@ cr_compress_file_with_stat(const char *src, char buf[BUFFER_SIZE]; CR_FILE *orig = NULL; CR_FILE *new = NULL; - gchar *dst = (gchar *) *in_dst; + gchar *dst = (gchar *) in_dst; GError *tmp_err = NULL; assert(src); @@ -476,15 +476,10 @@ cr_compress_file_with_stat(const char *src, if (!dst) { // If destination is NULL, use src + compression suffix - *in_dst = g_strconcat(src, - c_suffix, - NULL); + dst = g_strconcat(src, c_suffix, NULL); } else if (g_str_has_suffix(dst, "/")) { // If destination is dir use filename from src + compression suffix - *in_dst = g_strconcat(dst, - cr_get_filename(src), - c_suffix, - NULL); + dst = g_strconcat(dst, cr_get_filename(src), c_suffix, NULL); } else if (c_suffix && !g_str_has_suffix(dst, c_suffix)) { cr_CompressionType old_type = cr_detect_compression(src, &tmp_err); if (tmp_err) { @@ -492,14 +487,11 @@ cr_compress_file_with_stat(const char *src, g_clear_error(&tmp_err); } else if (old_type != CR_CW_NO_COMPRESSION) { _cleanup_free_ gchar *tmp_file = g_strndup(dst, strlen(dst) - strlen(cr_compression_suffix(old_type))); - *in_dst = g_strconcat(tmp_file, - c_suffix, - NULL); + dst = g_strconcat(tmp_file, + c_suffix, + NULL); } } - if (dst != *in_dst && dst) - g_free(dst); - dst = (gchar *) *in_dst; int mode = CR_CW_AUTO_DETECT_COMPRESSION; @@ -571,6 +563,9 @@ cr_compress_file_with_stat(const char *src, compress_file_cleanup: + if (dst != in_dst) + g_free(dst); + if (orig) cr_close(orig, NULL); @@ -186,7 +186,7 @@ gboolean cr_copy_file(const char *src, /** Compress file. * @param src source filename - * @param dst pointer to destination (If dst is dir, filename of src + + * @param dst destination (If dst is dir, filename of src + * compression suffix is used. * If dst is NULL, src + compression suffix is used) * @param comtype type of compression @@ -197,7 +197,7 @@ gboolean cr_copy_file(const char *src, * @return cr_Error return code */ int cr_compress_file_with_stat(const char *src, - char **dst, + const char *dst, cr_CompressionType comtype, cr_ContentStat *stat, const char *zck_dict_dir, diff --git a/src/modifyrepo_shared.c b/src/modifyrepo_shared.c index f9e24b2..4e59660 100644 --- a/src/modifyrepo_shared.c +++ b/src/modifyrepo_shared.c @@ -120,7 +120,7 @@ cr_write_file(gchar *repopath, cr_ModifyRepoTask *task, g_debug("%s: Copy & compress operation %s -> %s", __func__, src_fn, dst_fn); - if (cr_compress_file(src_fn, &dst_fn, compress_type, + if (cr_compress_file(src_fn, dst_fn, compress_type, task->zck_dict_dir, TRUE, err) != CRE_OK) { g_debug("%s: Copy & compress operation failed", __func__); return NULL; diff --git a/src/python/misc-py.c b/src/python/misc-py.c index 5daff48..6a7871e 100644 --- a/src/python/misc-py.c +++ b/src/python/misc-py.c @@ -49,7 +49,7 @@ py_compress_file_with_stat(G_GNUC_UNUSED PyObject *self, PyObject *args) return NULL; } - cr_compress_file_with_stat(src, &dst, type, contentstat, NULL, FALSE, &tmp_err); + cr_compress_file_with_stat(src, dst, type, contentstat, NULL, FALSE, &tmp_err); if (tmp_err) { nice_exception(&tmp_err, NULL); return NULL; diff --git a/src/threads.c b/src/threads.c index d13fba2..9ef839d 100644 --- a/src/threads.c +++ b/src/threads.c @@ -101,7 +101,7 @@ cr_compressing_thread(gpointer data, G_GNUC_UNUSED gpointer user_data) NULL); cr_compress_file_with_stat(task->src, - &(task->dst), + task->dst, task->type, task->stat, task->zck_dict_dir, diff --git a/tests/test_misc.c b/tests/test_misc.c index 64218f4..6614809 100644 --- a/tests/test_misc.c +++ b/tests/test_misc.c @@ -548,7 +548,7 @@ compressfile_test_text_file(Copyfiletest *copyfiletest, GError *tmp_err = NULL; g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); - ret = cr_compress_file(TEST_TEXT_FILE, &(copyfiletest->dst_file), + ret = cr_compress_file(TEST_TEXT_FILE, copyfiletest->dst_file, CR_CW_GZ_COMPRESSION, NULL, FALSE, &tmp_err); g_assert(!tmp_err); g_assert_cmpint(ret, ==, CRE_OK); @@ -574,7 +574,7 @@ compressfile_with_stat_test_text_file(Copyfiletest *copyfiletest, g_assert(!tmp_err); g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); - ret = cr_compress_file_with_stat(TEST_TEXT_FILE, ©filetest->dst_file, + ret = cr_compress_file_with_stat(TEST_TEXT_FILE, copyfiletest->dst_file, CR_CW_GZ_COMPRESSION, stat, NULL, FALSE, &tmp_err); g_assert(!tmp_err); @@ -600,23 +600,26 @@ compressfile_with_stat_test_gz_file_gz_output(Copyfiletest *copyfiletest, g_assert(stat); g_assert(!tmp_err); - g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); - ret = cr_compress_file_with_stat(TEST_TEXT_FILE_GZ, ©filetest->dst_file, + char * dst_full_name = g_strconcat(copyfiletest->dst_file, ".gz", NULL); + + g_assert(!g_file_test(dst_full_name, G_FILE_TEST_EXISTS)); + ret = cr_compress_file_with_stat(TEST_TEXT_FILE_GZ, dst_full_name, CR_CW_GZ_COMPRESSION, stat, NULL, FALSE, &tmp_err); g_assert(!tmp_err); g_assert_cmpint(ret, ==, CRE_OK); - g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR)); + g_assert(g_file_test(dst_full_name, G_FILE_TEST_IS_REGULAR)); checksum = cr_checksum_file(TEST_TEXT_FILE, CR_CHECKSUM_SHA256, NULL); g_assert_cmpstr(stat->checksum, ==, checksum); //assert content is readable after decompression and recompression char buf[30]; - read_file(copyfiletest->dst_file, CR_CW_GZ_COMPRESSION, buf, 30); + read_file(dst_full_name, CR_CW_GZ_COMPRESSION, buf, 30); g_assert(g_strrstr(buf, "Lorem ipsum dolor sit amet")); cr_contentstat_free(stat, &tmp_err); g_assert(!tmp_err); + free(dst_full_name); } @@ -626,20 +629,24 @@ compressfile_test_gz_file_xz_output(Copyfiletest *copyfiletest, { int ret; GError *tmp_err = NULL; - g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); - ret = cr_compress_file(TEST_TEXT_FILE_GZ, ©filetest->dst_file, + + char * dst_full_name = g_strconcat(copyfiletest->dst_file, ".xz", NULL); + + g_assert(!g_file_test(dst_full_name, G_FILE_TEST_EXISTS)); + ret = cr_compress_file(TEST_TEXT_FILE_GZ, dst_full_name, CR_CW_XZ_COMPRESSION, NULL, FALSE, &tmp_err); g_assert(!tmp_err); g_assert_cmpint(ret, ==, CRE_OK); - g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR)); + g_assert(g_file_test(dst_full_name, G_FILE_TEST_IS_REGULAR)); //assert content is readable after decompression and recompression char buf[30]; - read_file(copyfiletest->dst_file, CR_CW_XZ_COMPRESSION, buf, 30); + read_file(dst_full_name, CR_CW_XZ_COMPRESSION, buf, 30); g_assert(g_strrstr(buf, "Lorem ipsum dolor sit amet")); g_assert(!tmp_err); + free(dst_full_name); } @@ -649,20 +656,24 @@ compressfile_test_xz_file_gz_output(Copyfiletest *copyfiletest, { int ret; GError *tmp_err = NULL; - g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); - ret = cr_compress_file(TEST_TEXT_FILE_XZ, ©filetest->dst_file, + + char * dst_full_name = g_strconcat(copyfiletest->dst_file, ".gz", NULL); + + g_assert(!g_file_test(dst_full_name, G_FILE_TEST_EXISTS)); + ret = cr_compress_file(TEST_TEXT_FILE_XZ, dst_full_name, CR_CW_GZ_COMPRESSION, NULL, FALSE, &tmp_err); g_assert(!tmp_err); g_assert_cmpint(ret, ==, CRE_OK); - g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR)); + g_assert(g_file_test(dst_full_name, G_FILE_TEST_IS_REGULAR)); //assert content is readable after decompression and recompression char buf[30]; - read_file(copyfiletest->dst_file, CR_CW_GZ_COMPRESSION, buf, 30); + read_file(dst_full_name, CR_CW_GZ_COMPRESSION, buf, 30); g_assert(g_strrstr(buf, "Lorem ipsum dolor sit amet")); g_assert(!tmp_err); + free(dst_full_name); } @@ -672,13 +683,16 @@ compressfile_test_sqlite_file_gz_output(Copyfiletest *copyfiletest, { int ret; GError *tmp_err = NULL; - g_assert(!g_file_test(copyfiletest->dst_file, G_FILE_TEST_EXISTS)); - ret = cr_compress_file(TEST_SQLITE_FILE, ©filetest->dst_file, + + char * dst_full_name = g_strconcat(copyfiletest->dst_file, ".gz", NULL); + + g_assert(!g_file_test(dst_full_name, G_FILE_TEST_EXISTS)); + ret = cr_compress_file(TEST_SQLITE_FILE, dst_full_name, CR_CW_GZ_COMPRESSION, NULL, FALSE, &tmp_err); g_assert(!tmp_err); g_assert_cmpint(ret, ==, CRE_OK); - g_assert(g_file_test(copyfiletest->dst_file, G_FILE_TEST_IS_REGULAR)); + g_assert(g_file_test(dst_full_name, G_FILE_TEST_EXISTS)); g_assert(!tmp_err); } |