From 957a66e58444a2ac4bb77d978fd08e84fceffc38 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Fri, 30 Jul 2021 09:45:01 +0200 Subject: osl::Mutex->std::mutex in SequenceInputStream also (*) check params before taking mutex to minimise the time holding the mutex (*) some methods were missing a lock_guard Change-Id: Iac35328e67f81b38c896f7c1e2a3514b813656c1 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/119696 Tested-by: Jenkins Reviewed-by: Noel Grandin --- comphelper/source/streaming/seqstream.cxx | 23 ++++++++++++++--------- include/comphelper/seqstream.hxx | 5 +++-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/comphelper/source/streaming/seqstream.cxx b/comphelper/source/streaming/seqstream.cxx index 7cffb774232f..b33c63f0931b 100644 --- a/comphelper/source/streaming/seqstream.cxx +++ b/comphelper/source/streaming/seqstream.cxx @@ -57,13 +57,13 @@ inline sal_Int32 SequenceInputStream::avail() sal_Int32 SAL_CALL SequenceInputStream::readBytes( Sequence& aData, sal_Int32 nBytesToRead ) { - ::osl::MutexGuard aGuard( m_aMutex ); - - sal_Int32 nAvail = avail(); - if (nBytesToRead < 0) throw BufferSizeExceededException(OUString(),*this); + std::lock_guard aGuard( m_aMutex ); + + sal_Int32 nAvail = avail(); + if (nAvail < nBytesToRead) nBytesToRead = nAvail; @@ -84,13 +84,13 @@ sal_Int32 SAL_CALL SequenceInputStream::readSomeBytes( Sequence& aData void SAL_CALL SequenceInputStream::skipBytes( sal_Int32 nBytesToSkip ) { - ::osl::MutexGuard aGuard( m_aMutex ); - - sal_Int32 nAvail = avail(); - if (nBytesToSkip < 0) throw BufferSizeExceededException(OUString(),*this); + std::lock_guard aGuard( m_aMutex ); + + sal_Int32 nAvail = avail(); + if (nAvail < nBytesToSkip) nBytesToSkip = nAvail; @@ -100,7 +100,7 @@ void SAL_CALL SequenceInputStream::skipBytes( sal_Int32 nBytesToSkip ) sal_Int32 SAL_CALL SequenceInputStream::available( ) { - ::osl::MutexGuard aGuard( m_aMutex ); + std::lock_guard aGuard( m_aMutex ); return avail(); } @@ -108,6 +108,8 @@ sal_Int32 SAL_CALL SequenceInputStream::available( ) void SAL_CALL SequenceInputStream::closeInput( ) { + std::lock_guard aGuard( m_aMutex ); + if (m_nPos == -1) throw NotConnectedException(OUString(), *this); @@ -118,16 +120,19 @@ void SAL_CALL SequenceInputStream::seek( sal_Int64 location ) { if ( location > m_aData.getLength() || location < 0 || location > SAL_MAX_INT32 ) throw IllegalArgumentException("bad location", static_cast(this), 1); + std::lock_guard aGuard( m_aMutex ); m_nPos = static_cast(location); } sal_Int64 SAL_CALL SequenceInputStream::getPosition() { + std::lock_guard aGuard( m_aMutex ); return m_nPos; } sal_Int64 SAL_CALL SequenceInputStream::getLength( ) { + std::lock_guard aGuard( m_aMutex ); return m_aData.getLength(); } diff --git a/include/comphelper/seqstream.hxx b/include/comphelper/seqstream.hxx index 6ea3ebc2c777..88db729832c5 100644 --- a/include/comphelper/seqstream.hxx +++ b/include/comphelper/seqstream.hxx @@ -27,6 +27,7 @@ #include #include #include +#include namespace comphelper { @@ -39,7 +40,7 @@ namespace comphelper class COMPHELPER_DLLPUBLIC SequenceInputStream final : public ::cppu::WeakImplHelper< css::io::XInputStream, css::io::XSeekable > { - ::osl::Mutex m_aMutex; + std::mutex m_aMutex; css::uno::Sequence const m_aData; sal_Int32 m_nPos; @@ -62,7 +63,7 @@ public: virtual sal_Int64 SAL_CALL getLength( ) override; private: - inline sal_Int32 avail(); + sal_Int32 avail(); }; // don't export to avoid duplicate WeakImplHelper definitions with MSVC -- cgit