From f981c51eeabeb9789c9969283624f4148ac7dfd5 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Mon, 2 Jan 2023 22:22:47 -0800 Subject: [PATCH] get rid of -fanalyzer memory leaks Don't use make_shared inside a function. Instead, change constructor to value to have std::move. Also move shared_ptrs everywhere. It's fairly expensive to copy. Signed-off-by: Rosen Penev --- src/tiffcomposite_int.cpp | 18 ++++++++++-------- src/tiffcomposite_int.hpp | 6 +++--- src/tiffvisitor_int.cpp | 5 +++-- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/tiffcomposite_int.cpp b/src/tiffcomposite_int.cpp index 5eec994a04..1fcc55bfb5 100644 --- a/src/tiffcomposite_int.cpp +++ b/src/tiffcomposite_int.cpp @@ -212,16 +212,16 @@ int TiffEntryBase::idx() const { return idx_; } -void TiffEntryBase::setData(const std::shared_ptr& buf) { - storage_ = buf; - pData_ = buf->data(); - size_ = buf->size(); +void TiffEntryBase::setData(std::shared_ptr buf) { + storage_ = std::move(buf); + pData_ = storage_->data(); + size_ = storage_->size(); } -void TiffEntryBase::setData(byte* pData, size_t size, const std::shared_ptr& storage) { +void TiffEntryBase::setData(byte* pData, size_t size, std::shared_ptr storage) { pData_ = pData; size_ = size; - storage_ = storage; + storage_ = std::move(storage); if (!pData_) size_ = 0; } @@ -231,7 +231,8 @@ void TiffEntryBase::updateValue(Value::UniquePtr value, ByteOrder byteOrder) { return; size_t newSize = value->size(); if (newSize > size_) { - setData(std::make_shared(newSize)); + auto d = std::make_shared(newSize); + setData(std::move(d)); } if (pData_) { memset(pData_, 0x0, size_); @@ -431,7 +432,8 @@ size_t TiffBinaryArray::addElement(size_t idx, const ArrayDef& def) { // The assertion typically fails if a component is not configured in // the TIFF structure table (TiffCreator::tiffTreeStruct_) tp->setStart(pData() + idx); - tp->setData(const_cast(pData() + idx), sz, storage()); + auto s = storage(); + tp->setData(const_cast(pData() + idx), sz, std::move(s)); tp->setElDef(def); tp->setElByteOrder(cfg()->byteOrder_); addChild(std::move(tc)); diff --git a/src/tiffcomposite_int.hpp b/src/tiffcomposite_int.hpp index 92bd12fcbd..e198ea593a 100644 --- a/src/tiffcomposite_int.hpp +++ b/src/tiffcomposite_int.hpp @@ -428,13 +428,13 @@ class TiffEntryBase : public TiffComponent { you should pass std::shared_ptr(), which is essentially a nullptr. */ - void setData(byte* pData, size_t size, const std::shared_ptr& storage); + void setData(byte* pData, size_t size, std::shared_ptr storage); /*! @brief Set the entry's data buffer. A shared_ptr is used to manage the DataBuf because TiffEntryBase has a clone method so it is possible (in theory) for the DataBuf to have multiple owners. */ - void setData(const std::shared_ptr& buf); + void setData(std::shared_ptr buf); /*! @brief Update the value. Takes ownership of the pointer passed in. @@ -534,7 +534,7 @@ class TiffEntryBase : public TiffComponent { static size_t writeOffset(byte* buf, size_t offset, TiffType tiffType, ByteOrder byteOrder); //! Used (internally) to create another reference to the DataBuf reference by storage_. - [[nodiscard]] const std::shared_ptr& storage() const { + [[nodiscard]] std::shared_ptr storage() const { return storage_; } diff --git a/src/tiffvisitor_int.cpp b/src/tiffvisitor_int.cpp index 1ce34e4b24..1f501ee729 100644 --- a/src/tiffvisitor_int.cpp +++ b/src/tiffvisitor_int.cpp @@ -1329,7 +1329,8 @@ void TiffReader::readTiffEntry(TiffEntryBase* object) { v->read(pData, size, byteOrder()); object->setValue(std::move(v)); - object->setData(pData, size, std::make_shared()); + auto d = std::make_shared(); + object->setData(pData, size, std::move(d)); object->setOffset(offset); object->setIdx(nextIdx(object->group())); } catch (std::overflow_error&) { @@ -1374,7 +1375,7 @@ void TiffReader::visitBinaryArray(TiffBinaryArray* object) { size_t size = object->TiffEntryBase::doSize(); auto buf = std::make_shared(cryptFct(object->tag(), pData, size, pRoot_)); if (!buf->empty()) - object->setData(buf); + object->setData(std::move(buf)); } const ArrayDef* defs = object->def();