Skip to content

Commit c9cea75

Browse files
committed
change some unique to shared ptrs
Helps to avoid accidental leaks after dynamic cast. Signed-off-by: Rosen Penev <[email protected]>
1 parent f935e99 commit c9cea75

File tree

4 files changed

+27
-27
lines changed

4 files changed

+27
-27
lines changed

src/tiffcomposite_int.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -540,39 +540,38 @@ TiffComponent* TiffBinaryArray::doAddPath(uint16_t tag, TiffPath& tiffPath, Tiff
540540
return tc->addPath(tag, tiffPath, pRoot, std::move(object));
541541
} // TiffBinaryArray::doAddPath
542542

543-
TiffComponent* TiffComponent::addChild(TiffComponent::UniquePtr tiffComponent) {
543+
TiffComponent* TiffComponent::addChild(TiffComponent::SharedPtr tiffComponent) {
544544
return doAddChild(std::move(tiffComponent));
545545
} // TiffComponent::addChild
546546

547-
TiffComponent* TiffComponent::doAddChild(UniquePtr /*tiffComponent*/) {
547+
TiffComponent* TiffComponent::doAddChild(SharedPtr /*tiffComponent*/) {
548548
return nullptr;
549549
} // TiffComponent::doAddChild
550550

551-
TiffComponent* TiffDirectory::doAddChild(TiffComponent::UniquePtr tiffComponent) {
551+
TiffComponent* TiffDirectory::doAddChild(TiffComponent::SharedPtr tiffComponent) {
552552
return components_.emplace_back(std::move(tiffComponent)).get();
553553
} // TiffDirectory::doAddChild
554554

555-
TiffComponent* TiffSubIfd::doAddChild(TiffComponent::UniquePtr tiffComponent) {
556-
auto d = dynamic_cast<TiffDirectory*>(tiffComponent.get());
557-
if (!d) {
558-
throw Error(ErrorCode::kerErrorMessage, "dynamic_cast to TiffDirectory failed");
559-
}
560-
tiffComponent.release(); // NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks)
561-
return ifds_.emplace_back(d).get();
555+
TiffComponent* TiffSubIfd::doAddChild(TiffComponent::SharedPtr tiffComponent) {
556+
auto d = std::dynamic_pointer_cast<TiffDirectory>(std::move(tiffComponent));
557+
if (!d)
558+
throw Error(ErrorCode::kerErrorMessage, "dynamic_pointer_cast to TiffDirectory failed");
559+
560+
return ifds_.emplace_back(std::move(d)).get();
562561
} // TiffSubIfd::doAddChild
563562

564-
TiffComponent* TiffMnEntry::doAddChild(TiffComponent::UniquePtr tiffComponent) {
563+
TiffComponent* TiffMnEntry::doAddChild(TiffComponent::SharedPtr tiffComponent) {
565564
if (mn_) {
566565
return mn_->addChild(std::move(tiffComponent));
567566
}
568567
return nullptr;
569568
} // TiffMnEntry::doAddChild
570569

571-
TiffComponent* TiffIfdMakernote::doAddChild(TiffComponent::UniquePtr tiffComponent) {
570+
TiffComponent* TiffIfdMakernote::doAddChild(TiffComponent::SharedPtr tiffComponent) {
572571
return ifd_.addChild(std::move(tiffComponent));
573572
}
574573

575-
TiffComponent* TiffBinaryArray::doAddChild(TiffComponent::UniquePtr tiffComponent) {
574+
TiffComponent* TiffBinaryArray::doAddChild(TiffComponent::SharedPtr tiffComponent) {
576575
setDecoded(true);
577576
return elements_.emplace_back(std::move(tiffComponent)).get();
578577
} // TiffBinaryArray::doAddChild
@@ -1454,7 +1453,7 @@ TiffType toTiffType(TypeId typeId) {
14541453
return static_cast<TiffType>(typeId);
14551454
}
14561455

1457-
bool cmpTagLt(const TiffComponent::UniquePtr& lhs, const TiffComponent::UniquePtr& rhs) {
1456+
bool cmpTagLt(const TiffComponent::SharedPtr& lhs, const TiffComponent::SharedPtr& rhs) {
14581457
if (lhs->tag() != rhs->tag())
14591458
return lhs->tag() < rhs->tag();
14601459
return lhs->idx() < rhs->idx();

src/tiffcomposite_int.hpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ class TiffComponent {
153153
public:
154154
//! TiffComponent auto_ptr type
155155
using UniquePtr = std::unique_ptr<TiffComponent>;
156+
using SharedPtr = std::shared_ptr<TiffComponent>;
156157
//! Container type to hold all metadata
157-
using Components = std::vector<UniquePtr>;
158+
using Components = std::vector<SharedPtr>;
158159

159160
//! @name Creators
160161
//@{
@@ -185,7 +186,7 @@ class TiffComponent {
185186
@param tiffComponent Auto pointer to the component to add.
186187
@return Return a pointer to the newly added child element or 0.
187188
*/
188-
TiffComponent* addChild(UniquePtr tiffComponent);
189+
TiffComponent* addChild(SharedPtr tiffComponent);
189190
/*!
190191
@brief Add a "next" component to the component. Default is to do
191192
nothing.
@@ -295,7 +296,7 @@ class TiffComponent {
295296
//! Implements addPath(). The default implementation does nothing.
296297
virtual TiffComponent* doAddPath(uint16_t tag, TiffPath& tiffPath, TiffComponent* pRoot, UniquePtr object);
297298
//! Implements addChild(). The default implementation does nothing.
298-
virtual TiffComponent* doAddChild(UniquePtr tiffComponent);
299+
virtual TiffComponent* doAddChild(SharedPtr tiffComponent);
299300
//! Implements addNext(). The default implementation does nothing.
300301
virtual TiffComponent* doAddNext(UniquePtr tiffComponent);
301302
//! Implements accept().
@@ -853,7 +854,7 @@ class TiffDirectory : public TiffComponent {
853854
//! @name Protected Manipulators
854855
//@{
855856
TiffComponent* doAddPath(uint16_t tag, TiffPath& tiffPath, TiffComponent* pRoot, UniquePtr object) override;
856-
TiffComponent* doAddChild(UniquePtr tiffComponent) override;
857+
TiffComponent* doAddChild(SharedPtr tiffComponent) override;
857858
TiffComponent* doAddNext(UniquePtr tiffComponent) override;
858859
void doAccept(TiffVisitor& visitor) override;
859860
/*!
@@ -952,7 +953,7 @@ class TiffSubIfd : public TiffEntryBase {
952953
//! @name Protected Manipulators
953954
//@{
954955
TiffComponent* doAddPath(uint16_t tag, TiffPath& tiffPath, TiffComponent* pRoot, UniquePtr object) override;
955-
TiffComponent* doAddChild(UniquePtr tiffComponent) override;
956+
TiffComponent* doAddChild(SharedPtr tiffComponent) override;
956957
void doAccept(TiffVisitor& visitor) override;
957958
void doEncode(TiffEncoder& encoder, const Exifdatum* datum) override;
958959
/*!
@@ -988,7 +989,7 @@ class TiffSubIfd : public TiffEntryBase {
988989

989990
private:
990991
//! A collection of TIFF directories (IFDs)
991-
using Ifds = std::vector<std::unique_ptr<TiffDirectory>>;
992+
using Ifds = std::vector<std::shared_ptr<TiffDirectory>>;
992993

993994
// DATA
994995
IfdId newGroup_; //!< Start of the range of group numbers for the sub-IFDs
@@ -1019,7 +1020,7 @@ class TiffMnEntry : public TiffEntryBase {
10191020
//! @name Protected Manipulators
10201021
//@{
10211022
TiffComponent* doAddPath(uint16_t tag, TiffPath& tiffPath, TiffComponent* pRoot, UniquePtr object) override;
1022-
TiffComponent* doAddChild(UniquePtr tiffComponent) override;
1023+
TiffComponent* doAddChild(SharedPtr tiffComponent) override;
10231024
TiffComponent* doAddNext(UniquePtr tiffComponent) override;
10241025
void doAccept(TiffVisitor& visitor) override;
10251026
void doEncode(TiffEncoder& encoder, const Exifdatum* datum) override;
@@ -1145,7 +1146,7 @@ class TiffIfdMakernote : public TiffComponent {
11451146
//! @name Protected Manipulators
11461147
//@{
11471148
TiffComponent* doAddPath(uint16_t tag, TiffPath& tiffPath, TiffComponent* pRoot, UniquePtr object) override;
1148-
TiffComponent* doAddChild(UniquePtr tiffComponent) override;
1149+
TiffComponent* doAddChild(SharedPtr tiffComponent) override;
11491150
TiffComponent* doAddNext(UniquePtr tiffComponent) override;
11501151
void doAccept(TiffVisitor& visitor) override;
11511152
/*!
@@ -1348,7 +1349,7 @@ class TiffBinaryArray : public TiffEntryBase {
13481349
/*!
13491350
@brief Implements addChild(). Todo: Document it!
13501351
*/
1351-
TiffComponent* doAddChild(UniquePtr tiffComponent) override;
1352+
TiffComponent* doAddChild(SharedPtr tiffComponent) override;
13521353
void doAccept(TiffVisitor& visitor) override;
13531354
void doEncode(TiffEncoder& encoder, const Exifdatum* datum) override;
13541355
/*!
@@ -1471,7 +1472,7 @@ class TiffBinaryElement : public TiffEntryBase {
14711472
@brief Compare two TIFF component pointers by tag. Return true if the tag
14721473
of component lhs is less than that of rhs.
14731474
*/
1474-
bool cmpTagLt(const TiffComponent::UniquePtr& lhs, const TiffComponent::UniquePtr& rhs);
1475+
bool cmpTagLt(const TiffComponent::SharedPtr& lhs, const TiffComponent::SharedPtr& rhs);
14751476

14761477
//! Function to create and initialize a new TIFF entry
14771478
TiffComponent::UniquePtr newTiffEntry(uint16_t tag, IfdId group);

src/tiffvisitor_int.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,8 @@ void TiffEncoder::visitDirectoryNext(TiffDirectory* object) {
586586
}
587587
}
588588

589-
uint32_t TiffEncoder::updateDirEntry(byte* buf, ByteOrder byteOrder, const TiffComponent::UniquePtr& tiffComponent) {
590-
auto pTiffEntry = dynamic_cast<const TiffEntryBase*>(tiffComponent.get());
589+
uint32_t TiffEncoder::updateDirEntry(byte* buf, ByteOrder byteOrder, const TiffComponent::SharedPtr& tiffComponent) {
590+
auto pTiffEntry = std::dynamic_pointer_cast<TiffEntryBase>(tiffComponent);
591591
if (!pTiffEntry)
592592
return 0;
593593
us2Data(buf + 2, pTiffEntry->tiffType(), byteOrder);

src/tiffvisitor_int.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ class TiffEncoder : public TiffVisitor {
483483
entries are encoded. It takes care of type and count changes
484484
and size shrinkage for non-intrusive writing.
485485
*/
486-
static uint32_t updateDirEntry(byte* buf, ByteOrder byteOrder, const TiffComponent::UniquePtr& tiffComponent);
486+
static uint32_t updateDirEntry(byte* buf, ByteOrder byteOrder, const TiffComponent::SharedPtr& tiffComponent);
487487
/*!
488488
@brief Check if the tag is an image tag of an existing image. Such
489489
tags are copied from the original image and can't be modified.

0 commit comments

Comments
 (0)