You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "martinzink (via GitHub)" <gi...@apache.org> on 2024/10/15 06:32:28 UTC

[PR] MINIFICPP-2474 Fix memory leaks in extensions/libarchive [nifi-minifi-cpp]

martinzink opened a new pull request, #1880:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1880

   Thank you for submitting a contribution to Apache NiFi - MiNiFi C++.
   
   In order to streamline the review of the contribution we ask you
   to ensure the following steps have been taken:
   
   ### For all changes:
   - [ ] Is there a JIRA ticket associated with this PR? Is it referenced
        in the commit message?
   
   - [ ] Does your PR title start with MINIFICPP-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
   
   - [ ] Has your PR been rebased against the latest commit within the target branch (typically main)?
   
   - [ ] Is your initial contribution a single, squashed commit?
   
   ### For code changes:
   - [ ] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)?
   - [ ] If applicable, have you updated the LICENSE file?
   - [ ] If applicable, have you updated the NOTICE file?
   
   ### For documentation related changes:
   - [ ] Have you ensured that format looks appropriate for the output in which it is rendered?
   
   ### Note:
   Please ensure that once the PR is submitted, you check GitHub Actions CI results for build issues and submit an update to your PR as soon as possible.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINIFICPP-2474 Fix memory leaks in extensions/libarchive [nifi-minifi-cpp]

Posted by "szaszm (via GitHub)" <gi...@apache.org>.
szaszm closed pull request #1880: MINIFICPP-2474 Fix memory leaks in extensions/libarchive
URL: https://github.com/apache/nifi-minifi-cpp/pull/1880


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINIFICPP-2474 Fix memory leaks in extensions/libarchive [nifi-minifi-cpp]

Posted by "martinzink (via GitHub)" <gi...@apache.org>.
martinzink commented on code in PR #1880:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1880#discussion_r1806397388


##########
extensions/libarchive/tests/util/ArchiveTests.cpp:
##########
@@ -86,46 +87,43 @@ void build_test_archive(const std::filesystem::path& path, const TAE_MAP_T& entr
 
     std::cout << "Adding entry: " << name << std::endl;
 
-    archive_entry_set_filetype(entry, test_entry.type);
-    archive_entry_set_pathname(entry, test_entry.name.c_str());
-    archive_entry_set_size(entry, gsl::narrow<la_int64_t>(test_entry.size));
-    archive_entry_set_perm(entry, test_entry.perms);
-    archive_entry_set_uid(entry, test_entry.uid);
-    archive_entry_set_gid(entry, test_entry.gid);
-    archive_entry_set_mtime(entry, test_entry.mtime, test_entry.mtime_nsec);
+    archive_entry_set_filetype(entry.get(), test_entry.type);
+    archive_entry_set_pathname(entry.get(), test_entry.name.c_str());
+    archive_entry_set_size(entry.get(), gsl::narrow<la_int64_t>(test_entry.size));
+    archive_entry_set_perm(entry.get(), test_entry.perms);
+    archive_entry_set_uid(entry.get(), test_entry.uid);
+    archive_entry_set_gid(entry.get(), test_entry.gid);
+    archive_entry_set_mtime(entry.get(), test_entry.mtime, test_entry.mtime_nsec);
 
-    archive_write_header(test_archive, entry);
-    archive_write_data(test_archive, test_entry.content, test_entry.size);
+    archive_write_header(test_archive.get(), entry.get());
+    archive_write_data(test_archive.get(), test_entry.content, test_entry.size);
 
-    archive_entry_clear(entry);
+    archive_entry_clear(entry.get());
   }
-
-  archive_entry_free(entry);
-  archive_write_close(test_archive);
 }
 
-void build_test_archive(const std::filesystem::path& path, OrderedTestArchive& ordered_archive) {
+void build_test_archive(const std::filesystem::path& path, const OrderedTestArchive& ordered_archive) {
   build_test_archive(path, ordered_archive.map, ordered_archive.order);
 }
 
 bool check_archive_contents(const std::filesystem::path& path, const TAE_MAP_T& entries, bool check_attributes, const FN_VEC_T& order) {
   FN_VEC_T read_names;
   FN_VEC_T extra_names;
   bool ok = true;
-  struct archive *a = archive_read_new();
+  auto a = minifi::processors::archive_read_unique_ptr{archive_read_new()};
   struct archive_entry *entry = nullptr;
 
-  archive_read_support_format_all(a);
-  archive_read_support_filter_all(a);
+  archive_read_support_format_all(a.get());
+  archive_read_support_filter_all(a.get());
 
-  int r = archive_read_open_filename(a, path.string().c_str(), 16384);
+  int r = archive_read_open_filename(a.get(), path.string().c_str(), 16384);

Review Comment:
   👍 [review changes](https://github.com/apache/nifi-minifi-cpp/pull/1880/commits/3e748c09d8bd829d5e2b968ba9fda7108ae8f311)



##########
extensions/libarchive/tests/util/ArchiveTests.cpp:
##########
@@ -86,46 +87,43 @@ void build_test_archive(const std::filesystem::path& path, const TAE_MAP_T& entr
 
     std::cout << "Adding entry: " << name << std::endl;
 
-    archive_entry_set_filetype(entry, test_entry.type);
-    archive_entry_set_pathname(entry, test_entry.name.c_str());
-    archive_entry_set_size(entry, gsl::narrow<la_int64_t>(test_entry.size));
-    archive_entry_set_perm(entry, test_entry.perms);
-    archive_entry_set_uid(entry, test_entry.uid);
-    archive_entry_set_gid(entry, test_entry.gid);
-    archive_entry_set_mtime(entry, test_entry.mtime, test_entry.mtime_nsec);
+    archive_entry_set_filetype(entry.get(), test_entry.type);
+    archive_entry_set_pathname(entry.get(), test_entry.name.c_str());
+    archive_entry_set_size(entry.get(), gsl::narrow<la_int64_t>(test_entry.size));
+    archive_entry_set_perm(entry.get(), test_entry.perms);
+    archive_entry_set_uid(entry.get(), test_entry.uid);
+    archive_entry_set_gid(entry.get(), test_entry.gid);
+    archive_entry_set_mtime(entry.get(), test_entry.mtime, test_entry.mtime_nsec);
 
-    archive_write_header(test_archive, entry);
-    archive_write_data(test_archive, test_entry.content, test_entry.size);
+    archive_write_header(test_archive.get(), entry.get());
+    archive_write_data(test_archive.get(), test_entry.content, test_entry.size);
 
-    archive_entry_clear(entry);
+    archive_entry_clear(entry.get());
   }
-
-  archive_entry_free(entry);
-  archive_write_close(test_archive);
 }
 
-void build_test_archive(const std::filesystem::path& path, OrderedTestArchive& ordered_archive) {
+void build_test_archive(const std::filesystem::path& path, const OrderedTestArchive& ordered_archive) {
   build_test_archive(path, ordered_archive.map, ordered_archive.order);
 }
 
 bool check_archive_contents(const std::filesystem::path& path, const TAE_MAP_T& entries, bool check_attributes, const FN_VEC_T& order) {
   FN_VEC_T read_names;
   FN_VEC_T extra_names;
   bool ok = true;
-  struct archive *a = archive_read_new();
+  auto a = minifi::processors::archive_read_unique_ptr{archive_read_new()};

Review Comment:
   👍 [review changes](https://github.com/apache/nifi-minifi-cpp/pull/1880/commits/3e748c09d8bd829d5e2b968ba9fda7108ae8f311)



##########
extensions/libarchive/tests/MergeFileTests.cpp:
##########
@@ -110,34 +112,34 @@ class FixedBuffer {
   }
 
  private:
-  std::unique_ptr<uint8_t[]> buf_;  // NOLINT(cppcoreguidelines-avoid-c-arrays)
+  std::vector<uint8_t> buf_;
   std::size_t size_ = 0;
   std::size_t capacity_ = 0;
 };
 
 std::vector<FixedBuffer> read_archives(const FixedBuffer& input) {
   class ArchiveEntryReader {
    public:
-    explicit ArchiveEntryReader(archive* arch) : arch(arch) {}
-    size_t read(std::span<std::byte> out_buffer) {
-      const auto ret = archive_read_data(arch, out_buffer.data(), out_buffer.size());
+    explicit ArchiveEntryReader(archive& arch) : arch(arch) {}
+    [[nodiscard]] size_t read(std::span<std::byte> out_buffer) const {
+      const auto ret = archive_read_data(&arch, out_buffer.data(), out_buffer.size());
       return ret < 0 ? minifi::io::STREAM_ERROR : gsl::narrow<size_t>(ret);
     }
    private:
-    archive* arch;
+    archive& arch;
   };
   std::vector<FixedBuffer> archive_contents;
-  struct archive *a = archive_read_new();
-  archive_read_support_format_all(a);
-  archive_read_support_filter_all(a);
-  archive_read_open_memory(a, input.begin(), input.size());
+  const auto a = minifi::processors::archive_read_unique_ptr{archive_read_new()};

Review Comment:
   👍 [review changes](https://github.com/apache/nifi-minifi-cpp/pull/1880/commits/3e748c09d8bd829d5e2b968ba9fda7108ae8f311)



##########
extensions/libarchive/tests/CompressContentTests.cpp:
##########
@@ -69,18 +68,17 @@ class ReadCallback {
     return total_read;
   }
   void archive_read() {
-    struct archive_read_deleter { void operator()(struct archive* p) const noexcept { archive_read_free(p); } };
-    std::unique_ptr<struct archive, archive_read_deleter> a{archive_read_new()};
+    auto a = minifi::processors::archive_read_unique_ptr{archive_read_new()};
     archive_read_support_format_all(a.get());
     archive_read_support_filter_all(a.get());
     archive_read_open_memory(a.get(), buffer_.data(), read_size_);
     struct archive_entry *ae = nullptr;

Review Comment:
   👍 [review changes](https://github.com/apache/nifi-minifi-cpp/pull/1880/commits/3e748c09d8bd829d5e2b968ba9fda7108ae8f311)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINIFICPP-2474 Fix memory leaks in extensions/libarchive [nifi-minifi-cpp]

Posted by "lordgamez (via GitHub)" <gi...@apache.org>.
lordgamez commented on code in PR #1880:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1880#discussion_r1806457676


##########
extensions/libarchive/tests/CompressContentTests.cpp:
##########
@@ -69,18 +68,17 @@ class ReadCallback {
     return total_read;
   }
   void archive_read() {
-    struct archive_read_deleter { void operator()(struct archive* p) const noexcept { archive_read_free(p); } };
-    std::unique_ptr<struct archive, archive_read_deleter> a{archive_read_new()};
+    auto a = minifi::processors::archive_read_unique_ptr{archive_read_new()};
     archive_read_support_format_all(a.get());
     archive_read_support_filter_all(a.get());
     archive_read_open_memory(a.get(), buffer_.data(), read_size_);
     struct archive_entry *ae = nullptr;

Review Comment:
   Instances of "ae" should still be changed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINIFICPP-2474 Fix memory leaks in extensions/libarchive [nifi-minifi-cpp]

Posted by "lordgamez (via GitHub)" <gi...@apache.org>.
lordgamez commented on code in PR #1880:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1880#discussion_r1802841114


##########
extensions/libarchive/tests/util/ArchiveTests.cpp:
##########
@@ -86,46 +87,43 @@ void build_test_archive(const std::filesystem::path& path, const TAE_MAP_T& entr
 
     std::cout << "Adding entry: " << name << std::endl;
 
-    archive_entry_set_filetype(entry, test_entry.type);
-    archive_entry_set_pathname(entry, test_entry.name.c_str());
-    archive_entry_set_size(entry, gsl::narrow<la_int64_t>(test_entry.size));
-    archive_entry_set_perm(entry, test_entry.perms);
-    archive_entry_set_uid(entry, test_entry.uid);
-    archive_entry_set_gid(entry, test_entry.gid);
-    archive_entry_set_mtime(entry, test_entry.mtime, test_entry.mtime_nsec);
+    archive_entry_set_filetype(entry.get(), test_entry.type);
+    archive_entry_set_pathname(entry.get(), test_entry.name.c_str());
+    archive_entry_set_size(entry.get(), gsl::narrow<la_int64_t>(test_entry.size));
+    archive_entry_set_perm(entry.get(), test_entry.perms);
+    archive_entry_set_uid(entry.get(), test_entry.uid);
+    archive_entry_set_gid(entry.get(), test_entry.gid);
+    archive_entry_set_mtime(entry.get(), test_entry.mtime, test_entry.mtime_nsec);
 
-    archive_write_header(test_archive, entry);
-    archive_write_data(test_archive, test_entry.content, test_entry.size);
+    archive_write_header(test_archive.get(), entry.get());
+    archive_write_data(test_archive.get(), test_entry.content, test_entry.size);
 
-    archive_entry_clear(entry);
+    archive_entry_clear(entry.get());
   }
-
-  archive_entry_free(entry);
-  archive_write_close(test_archive);
 }
 
-void build_test_archive(const std::filesystem::path& path, OrderedTestArchive& ordered_archive) {
+void build_test_archive(const std::filesystem::path& path, const OrderedTestArchive& ordered_archive) {
   build_test_archive(path, ordered_archive.map, ordered_archive.order);
 }
 
 bool check_archive_contents(const std::filesystem::path& path, const TAE_MAP_T& entries, bool check_attributes, const FN_VEC_T& order) {
   FN_VEC_T read_names;
   FN_VEC_T extra_names;
   bool ok = true;
-  struct archive *a = archive_read_new();
+  auto a = minifi::processors::archive_read_unique_ptr{archive_read_new()};
   struct archive_entry *entry = nullptr;
 
-  archive_read_support_format_all(a);
-  archive_read_support_filter_all(a);
+  archive_read_support_format_all(a.get());
+  archive_read_support_filter_all(a.get());
 
-  int r = archive_read_open_filename(a, path.string().c_str(), 16384);
+  int r = archive_read_open_filename(a.get(), path.string().c_str(), 16384);

Review Comment:
   And this



##########
extensions/libarchive/tests/util/ArchiveTests.cpp:
##########
@@ -86,46 +87,43 @@ void build_test_archive(const std::filesystem::path& path, const TAE_MAP_T& entr
 
     std::cout << "Adding entry: " << name << std::endl;
 
-    archive_entry_set_filetype(entry, test_entry.type);
-    archive_entry_set_pathname(entry, test_entry.name.c_str());
-    archive_entry_set_size(entry, gsl::narrow<la_int64_t>(test_entry.size));
-    archive_entry_set_perm(entry, test_entry.perms);
-    archive_entry_set_uid(entry, test_entry.uid);
-    archive_entry_set_gid(entry, test_entry.gid);
-    archive_entry_set_mtime(entry, test_entry.mtime, test_entry.mtime_nsec);
+    archive_entry_set_filetype(entry.get(), test_entry.type);
+    archive_entry_set_pathname(entry.get(), test_entry.name.c_str());
+    archive_entry_set_size(entry.get(), gsl::narrow<la_int64_t>(test_entry.size));
+    archive_entry_set_perm(entry.get(), test_entry.perms);
+    archive_entry_set_uid(entry.get(), test_entry.uid);
+    archive_entry_set_gid(entry.get(), test_entry.gid);
+    archive_entry_set_mtime(entry.get(), test_entry.mtime, test_entry.mtime_nsec);
 
-    archive_write_header(test_archive, entry);
-    archive_write_data(test_archive, test_entry.content, test_entry.size);
+    archive_write_header(test_archive.get(), entry.get());
+    archive_write_data(test_archive.get(), test_entry.content, test_entry.size);
 
-    archive_entry_clear(entry);
+    archive_entry_clear(entry.get());
   }
-
-  archive_entry_free(entry);
-  archive_write_close(test_archive);
 }
 
-void build_test_archive(const std::filesystem::path& path, OrderedTestArchive& ordered_archive) {
+void build_test_archive(const std::filesystem::path& path, const OrderedTestArchive& ordered_archive) {
   build_test_archive(path, ordered_archive.map, ordered_archive.order);
 }
 
 bool check_archive_contents(const std::filesystem::path& path, const TAE_MAP_T& entries, bool check_attributes, const FN_VEC_T& order) {
   FN_VEC_T read_names;
   FN_VEC_T extra_names;
   bool ok = true;
-  struct archive *a = archive_read_new();
+  auto a = minifi::processors::archive_read_unique_ptr{archive_read_new()};

Review Comment:
   This too



##########
extensions/libarchive/tests/CompressContentTests.cpp:
##########
@@ -69,18 +68,17 @@ class ReadCallback {
     return total_read;
   }
   void archive_read() {
-    struct archive_read_deleter { void operator()(struct archive* p) const noexcept { archive_read_free(p); } };
-    std::unique_ptr<struct archive, archive_read_deleter> a{archive_read_new()};
+    auto a = minifi::processors::archive_read_unique_ptr{archive_read_new()};
     archive_read_support_format_all(a.get());
     archive_read_support_filter_all(a.get());
     archive_read_open_memory(a.get(), buffer_.data(), read_size_);
     struct archive_entry *ae = nullptr;

Review Comment:
   Could you please rename these from "a" and "ae" to something a bit more descriptive?



##########
extensions/libarchive/tests/MergeFileTests.cpp:
##########
@@ -110,34 +112,34 @@ class FixedBuffer {
   }
 
  private:
-  std::unique_ptr<uint8_t[]> buf_;  // NOLINT(cppcoreguidelines-avoid-c-arrays)
+  std::vector<uint8_t> buf_;
   std::size_t size_ = 0;
   std::size_t capacity_ = 0;
 };
 
 std::vector<FixedBuffer> read_archives(const FixedBuffer& input) {
   class ArchiveEntryReader {
    public:
-    explicit ArchiveEntryReader(archive* arch) : arch(arch) {}
-    size_t read(std::span<std::byte> out_buffer) {
-      const auto ret = archive_read_data(arch, out_buffer.data(), out_buffer.size());
+    explicit ArchiveEntryReader(archive& arch) : arch(arch) {}
+    [[nodiscard]] size_t read(std::span<std::byte> out_buffer) const {
+      const auto ret = archive_read_data(&arch, out_buffer.data(), out_buffer.size());
       return ret < 0 ? minifi::io::STREAM_ERROR : gsl::narrow<size_t>(ret);
     }
    private:
-    archive* arch;
+    archive& arch;
   };
   std::vector<FixedBuffer> archive_contents;
-  struct archive *a = archive_read_new();
-  archive_read_support_format_all(a);
-  archive_read_support_filter_all(a);
-  archive_read_open_memory(a, input.begin(), input.size());
+  const auto a = minifi::processors::archive_read_unique_ptr{archive_read_new()};

Review Comment:
   Rename this please as well



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINIFICPP-2474 Fix memory leaks in extensions/libarchive [nifi-minifi-cpp]

Posted by "martinzink (via GitHub)" <gi...@apache.org>.
martinzink commented on code in PR #1880:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1880#discussion_r1806480672


##########
extensions/libarchive/tests/CompressContentTests.cpp:
##########
@@ -69,18 +68,17 @@ class ReadCallback {
     return total_read;
   }
   void archive_read() {
-    struct archive_read_deleter { void operator()(struct archive* p) const noexcept { archive_read_free(p); } };
-    std::unique_ptr<struct archive, archive_read_deleter> a{archive_read_new()};
+    auto a = minifi::processors::archive_read_unique_ptr{archive_read_new()};
     archive_read_support_format_all(a.get());
     archive_read_support_filter_all(a.get());
     archive_read_open_memory(a.get(), buffer_.data(), read_size_);
     struct archive_entry *ae = nullptr;

Review Comment:
   [ae -> archive_entry](https://github.com/apache/nifi-minifi-cpp/pull/1880/commits/09a73c4230c51aefc9761ccd20a5119630b2345d)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org