From 3519697d0e72c03307ca3056f47c6d70a6d5987d Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 31 Aug 2021 12:54:51 +0200 Subject: [PATCH] [libcalamares] Slightly more memory-safe Use unique_ptr to ensure Private is always deleted. SEE #1758 --- src/libcalamares/partition/Mount.cpp | 7 ++----- src/libcalamares/partition/Mount.h | 6 ++++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/libcalamares/partition/Mount.cpp b/src/libcalamares/partition/Mount.cpp index 0fd204df4..89e17a885 100644 --- a/src/libcalamares/partition/Mount.cpp +++ b/src/libcalamares/partition/Mount.cpp @@ -92,7 +92,7 @@ struct TemporaryMount::Private TemporaryMount::TemporaryMount( const QString& devicePath, const QString& filesystemName, const QString& options ) - : m_d( new Private ) + : m_d( std::make_unique() ) { m_d->m_devicePath = devicePath; m_d->m_mountDir.setAutoRemove( false ); @@ -100,8 +100,7 @@ TemporaryMount::TemporaryMount( const QString& devicePath, const QString& filesy if ( r ) { cWarning() << "Mount of" << devicePath << "on" << m_d->m_mountDir.path() << "failed, code" << r; - delete m_d; - m_d = nullptr; + m_d.reset(); } } @@ -115,8 +114,6 @@ TemporaryMount::~TemporaryMount() cWarning() << "UnMount of temporary" << m_d->m_devicePath << "on" << m_d->m_mountDir.path() << "failed, code" << r; } - delete m_d; - m_d = nullptr; } } diff --git a/src/libcalamares/partition/Mount.h b/src/libcalamares/partition/Mount.h index 6a2ef9f8b..d088b108f 100644 --- a/src/libcalamares/partition/Mount.h +++ b/src/libcalamares/partition/Mount.h @@ -17,6 +17,8 @@ #include #include +#include + namespace CalamaresUtils { namespace Partition @@ -58,12 +60,12 @@ public: TemporaryMount& operator=( const TemporaryMount& ) = delete; ~TemporaryMount(); - bool isValid() const { return m_d; } + bool isValid() const { return bool( m_d ); } QString path() const; private: struct Private; - Private* m_d = nullptr; + std::unique_ptr< Private > m_d; }; } // namespace Partition