This is an automated email from the ASF dual-hosted git repository.

pengzheng pushed a commit to branch feature/refactor_bundle_cache
in repository https://gitbox.apache.org/repos/asf/celix.git


The following commit(s) were added to refs/heads/feature/refactor_bundle_cache 
by this push:
     new 9880b032 Fix memory leak when trying to extract bad embedded data.
9880b032 is described below

commit 9880b0329e000550cf3a86d6233e6fc637d92e97
Author: PengZheng <[email protected]>
AuthorDate: Tue Mar 28 14:57:41 2023 +0800

    Fix memory leak when trying to extract bad embedded data.
---
 libs/utils/gtest/src/FileUtilsTestSuite.cc | 15 +++++++++++++++
 libs/utils/src/celix_file_utils.c          |  4 +++-
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/libs/utils/gtest/src/FileUtilsTestSuite.cc 
b/libs/utils/gtest/src/FileUtilsTestSuite.cc
index 53ed616b..dc900fd0 100644
--- a/libs/utils/gtest/src/FileUtilsTestSuite.cc
+++ b/libs/utils/gtest/src/FileUtilsTestSuite.cc
@@ -22,6 +22,7 @@
 #include <string>
 #include <thread>
 #include <unistd.h>
+#include <vector>
 
 #include "celix_file_utils.h"
 #include "celix_properties.h"
@@ -232,6 +233,20 @@ TEST_F(FileUtilsTestSuite, ExtractZipDataTest) {
     EXPECT_EQ(celix_properties_getAsLong(props, "level", 0), 2);
     celix_properties_destroy(props);
 }
+
+TEST_F(FileUtilsTestSuite, ExtractBadZipDataTest) {
+    const char* extractLocation = "extract_location";
+    const char* file1 = "extract_location/top.properties";
+    const char* file2 = "extract_location/subdir/sub.properties";
+    celix_utils_deleteDirectory(extractLocation, nullptr);
+
+    EXPECT_FALSE(celix_utils_fileExists(extractLocation));
+    std::vector<uint8_t> zipData(test_data_start, 
test_data_start+(test_data_end-test_data_start)/2);
+    auto status = celix_utils_extractZipData(zipData.data(), zipData.size(), 
extractLocation, nullptr);
+    EXPECT_NE(status, CELIX_SUCCESS);
+    EXPECT_FALSE(celix_utils_fileExists(file1));
+    EXPECT_FALSE(celix_utils_fileExists(file2));
+}
 #endif
 
 TEST_F(FileUtilsTestSuite, LastModifiedTest) {
diff --git a/libs/utils/src/celix_file_utils.c 
b/libs/utils/src/celix_file_utils.c
index 945d7fe5..861f4439 100644
--- a/libs/utils/src/celix_file_utils.c
+++ b/libs/utils/src/celix_file_utils.c
@@ -268,6 +268,8 @@ celix_status_t celix_utils_extractZipData(const void 
*zipData, size_t zipDataSiz
     if (source) {
         zip = zip_open_from_source(source, 0, &zipError);
         if (zip) {
+            // so that we can call zip_source_free no matter whether 
zip_open_from_source succeeded or not
+            zip_source_keep(source);
             status = celix_utils_extractZipInternal(zip, extractToDir, 
errorOut);
         }
     }
@@ -280,7 +282,7 @@ celix_status_t celix_utils_extractZipData(const void 
*zipData, size_t zipDataSiz
         zip_close(zip);
     }
     if (source != NULL) {
-        zip_source_close(source);
+        zip_source_free(source);
     }
 
     return status;

Reply via email to