include/svl/SfxBroadcaster.hxx       |    2 +-
 package/inc/ZipFile.hxx              |    2 +-
 package/source/zipapi/ZipFile.cxx    |   22 +++++++++-------------
 svl/source/notify/SfxBroadcaster.cxx |    2 +-
 sw/source/core/unocore/unostyle.cxx  |   17 ++++++++++++-----
 5 files changed, 24 insertions(+), 21 deletions(-)

New commits:
commit a9d806a68f4d8b21d80b8c0f35cb3a38d4b460aa
Author:     Noel Grandin <[email protected]>
AuthorDate: Tue Aug 27 13:12:41 2024 +0200
Commit:     Noel Grandin <[email protected]>
CommitDate: Tue Aug 27 17:24:44 2024 +0200

    tdf#158556 prevent lambda from allocating on heap
    
    bit of a hack
    
    Change-Id: Icf60fc1bc69e8c44e5612c05469164439f14a249
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/172465
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <[email protected]>

diff --git a/include/svl/SfxBroadcaster.hxx b/include/svl/SfxBroadcaster.hxx
index f4ba2264a066..e4b0f46cdeff 100644
--- a/include/svl/SfxBroadcaster.hxx
+++ b/include/svl/SfxBroadcaster.hxx
@@ -55,7 +55,7 @@ public:
 
     /** Iterate over all the listeners and call the passed function.
         return true to break the loop.  */
-    void  ForAllListeners(std::function<bool(SfxListener*)> f) const;
+    void  ForAllListeners(const std::function<bool(SfxListener*)>& f) const;
 
     /** used to avoid dynamic_cast cost */
     virtual bool IsSfxStyleSheet() const;
diff --git a/svl/source/notify/SfxBroadcaster.cxx 
b/svl/source/notify/SfxBroadcaster.cxx
index c9a26aabb7c8..a09de0377f81 100644
--- a/svl/source/notify/SfxBroadcaster.cxx
+++ b/svl/source/notify/SfxBroadcaster.cxx
@@ -131,7 +131,7 @@ void SfxBroadcaster::RemoveListener(SfxListener& rListener)
     m_RemovedPositions.push_back(positionOfRemovedElement);
 }
 
-void SfxBroadcaster::ForAllListeners(std::function<bool(SfxListener*)> f) const
+void SfxBroadcaster::ForAllListeners(const std::function<bool(SfxListener*)>& 
f) const
 {
     for (size_t i = 0; i < m_Listeners.size(); ++i)
     {
diff --git a/sw/source/core/unocore/unostyle.cxx 
b/sw/source/core/unocore/unostyle.cxx
index e1525ed25e1a..de77ddadf66d 100644
--- a/sw/source/core/unocore/unostyle.cxx
+++ b/sw/source/core/unocore/unostyle.cxx
@@ -1186,21 +1186,28 @@ uno::Any SAL_CALL SwXStyleFamily::getPropertyValue( 
const OUString& sPropertyNam
 
 SwXStyle* SwXStyleFamily::FindStyle(std::u16string_view rStyleName) const
 {
-    SwXStyle* pFoundStyle = nullptr;
+    // put params for lambda into struct, so that the lambda does not allocate 
memory on the heap.
+    struct MyParams {
+        const StyleFamilyEntry& m_rEntry;
+        SwXStyle* pFoundStyle;
+        std::u16string_view aStyleName;
+    } aParams { m_rEntry, nullptr, rStyleName };
     m_pBasePool->ForAllListeners(
-        [this, &pFoundStyle, &rStyleName] (SfxListener* pListener)
+        [&aParams] (SfxListener* pListener)
         {
             if (!pListener->IsSwXStyle())
                 return false;
             SwXStyle* pTempStyle = static_cast<SwXStyle*>(pListener);
-            if(pTempStyle && pTempStyle->GetFamily() == m_rEntry.family() && 
pTempStyle->GetStyleName() == rStyleName)
+            if(pTempStyle
+                && pTempStyle->GetFamily() == aParams.m_rEntry.family()
+                && pTempStyle->GetStyleName() == aParams.aStyleName)
             {
-                pFoundStyle = pTempStyle;
+                aParams.pFoundStyle = pTempStyle;
                 return true; // break
             }
             return false;
         });
-    return pFoundStyle;
+    return aParams.pFoundStyle;
 }
 
 static SwGetPoolIdFromName lcl_GetSwEnumFromSfxEnum(SfxStyleFamily eFamily)
commit 1a47d1e00ddadce2c4dc9e6d10fc815f8c7d9e59
Author:     Noel Grandin <[email protected]>
AuthorDate: Tue Aug 27 11:53:38 2024 +0200
Commit:     Noel Grandin <[email protected]>
CommitDate: Tue Aug 27 17:24:36 2024 +0200

    tdf#158556 unnecessary temporary OUString in ZipFile
    
    Change-Id: Iea3bf5fa9ba5210eabca521316cfd68b440ec806
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/172458
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <[email protected]>

diff --git a/package/inc/ZipFile.hxx b/package/inc/ZipFile.hxx
index 65df2ad00a60..4704d0e09910 100644
--- a/package/inc/ZipFile.hxx
+++ b/package/inc/ZipFile.hxx
@@ -101,7 +101,7 @@ private:
     static bool readExtraFields(MemoryByteGrabber& aMemGrabber, sal_Int16 
nExtraLen,
                                 sal_uInt64& nSize, sal_uInt64& nCompressedSize,
                                 ::std::optional<sal_uInt64> & roOffset,
-                                OUString const* pCENFilenameToCheck);
+                                std::string_view const * pCENFilenameToCheck);
 
 public:
 
diff --git a/package/source/zipapi/ZipFile.cxx 
b/package/source/zipapi/ZipFile.cxx
index 37907647711b..706292148da3 100644
--- a/package/source/zipapi/ZipFile.cxx
+++ b/package/source/zipapi/ZipFile.cxx
@@ -979,12 +979,9 @@ sal_uInt64 ZipFile::readLOC(ZipEntry &rEntry)
         // coverity[tainted_data] - we've checked negative lens, and up to max 
short is ok here
         std::vector<sal_Int8> aNameBuffer(nPathLen);
         sal_Int32 nRead = aGrabber.readBytes(aNameBuffer.data(), nPathLen);
-        if (nRead < nPathLen)
-            aNameBuffer.resize(nRead);
+        std::string_view aNameView(reinterpret_cast<const char 
*>(aNameBuffer.data()), nRead);
 
-        OUString sLOCPath( reinterpret_cast<const char *>(aNameBuffer.data()),
-                           nRead,
-                           RTL_TEXTENCODING_UTF8 );
+        OUString sLOCPath( aNameView.data(), aNameView.size(), 
RTL_TEXTENCODING_UTF8 );
 
         if ( rEntry.nPathLen == -1 ) // the file was created
         {
@@ -1007,7 +1004,7 @@ sal_uInt64 ZipFile::readLOC(ZipEntry &rEntry)
             MemoryByteGrabber extraMemGrabber(aExtraBuffer.data(), nExtraLen);
 
             isZip64 = readExtraFields(extraMemGrabber, nExtraLen,
-                    nLocSize, nLocCompressedSize, oOffset64, &sLOCPath);
+                    nLocSize, nLocCompressedSize, oOffset64, &aNameView);
         }
 
         // Just plain ignore bits 1 & 2 of the flag field - they are either
@@ -1389,9 +1386,8 @@ sal_Int32 ZipFile::readCEN()
                 throw ZipException(u"name too long"_ustr);
 
             // read always in UTF8, some tools seem not to set UTF8 bit
-            aEntry.sPath = OUString( reinterpret_cast<char const 
*>(aMemGrabber.getCurrentPos()),
-                                     aEntry.nPathLen,
-                                     RTL_TEXTENCODING_UTF8 );
+            std::string_view aPathView(reinterpret_cast<char const 
*>(aMemGrabber.getCurrentPos()), aEntry.nPathLen);
+            aEntry.sPath = OUString( aPathView.data(), aPathView.size(), 
RTL_TEXTENCODING_UTF8 );
 
             if ( !::comphelper::OStorageHelper::IsValidZipEntryFileName( 
aEntry.sPath, true ) )
                 throw ZipException(u"Zip entry has an invalid name."_ustr );
@@ -1401,7 +1397,7 @@ sal_Int32 ZipFile::readCEN()
             if (aEntry.nExtraLen>0)
             {
                 ::std::optional<sal_uInt64> oOffset64;
-                readExtraFields(aMemGrabber, aEntry.nExtraLen, nSize, 
nCompressedSize, oOffset64, &aEntry.sPath);
+                readExtraFields(aMemGrabber, aEntry.nExtraLen, nSize, 
nCompressedSize, oOffset64, &aPathView);
                 if (oOffset64)
                 {
                     nOffset = *oOffset64;
@@ -1515,7 +1511,7 @@ sal_Int32 ZipFile::readCEN()
 bool ZipFile::readExtraFields(MemoryByteGrabber& aMemGrabber, sal_Int16 
nExtraLen,
         sal_uInt64& nSize, sal_uInt64& nCompressedSize,
         std::optional<sal_uInt64> & roOffset,
-        OUString const*const pCENFilenameToCheck)
+        std::string_view const * pCENFilenameToCheck)
 {
     bool isZip64{false};
     while (nExtraLen > 0) // Extensible data fields
@@ -1562,8 +1558,8 @@ bool ZipFile::readExtraFields(MemoryByteGrabber& 
aMemGrabber, sal_Int16 nExtraLe
             // is already converted to UTF-16 here)
             (void) aMemGrabber.ReadUInt32();
             // this is required to be UTF-8
-            OUString const unicodePath(reinterpret_cast<char const 
*>(aMemGrabber.getCurrentPos()),
-                    dataSize - 5, RTL_TEXTENCODING_UTF8);
+            std::string_view unicodePath(reinterpret_cast<char const 
*>(aMemGrabber.getCurrentPos()),
+                    dataSize - 5);
             aMemGrabber.skipBytes(dataSize - 5);
             if (unicodePath != *pCENFilenameToCheck)
             {

Reply via email to