sal/rtl/strtmpl.hxx |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

New commits:
commit 926095df6b444866cd7e245b6a7befa6c2e29424
Author:     Stephan Bergmann <sberg...@redhat.com>
AuthorDate: Sun Mar 19 22:50:46 2023 +0100
Commit:     Stephan Bergmann <sberg...@redhat.com>
CommitDate: Mon Mar 20 07:01:25 2023 +0000

    Shrink rtl_[u]String allocation size to what's actually needed
    
    Change-Id: Ib39caf31b5d2fb06cc81cdeb14578794b35d8ed4
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/149120
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sberg...@redhat.com>

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index a814f4998a43..ca2800c6a012 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -22,6 +22,7 @@
 #include <algorithm>
 #include <cassert>
 #include <cmath>
+#include <cstddef>
 #include <cstdlib>
 #include <cstring>
 #include <cwchar>
@@ -832,12 +833,13 @@ template <typename C> using STRINGDATA = typename 
STRINGDATA_<C>::T;
 
 template <typename IMPL_RTL_STRINGDATA> IMPL_RTL_STRINGDATA* Alloc( sal_Int32 
nLen )
 {
+    constexpr auto fix = offsetof(IMPL_RTL_STRINGDATA, buffer) + sizeof 
IMPL_RTL_STRINGDATA::buffer;
     IMPL_RTL_STRINGDATA * pData
         = (sal::static_int_cast< sal_uInt32 >(nLen)
-           <= ((SAL_MAX_UINT32 - sizeof (IMPL_RTL_STRINGDATA))
+           <= ((SAL_MAX_UINT32 - fix)
                / sizeof (STRCODE<IMPL_RTL_STRINGDATA>)))
         ? static_cast<IMPL_RTL_STRINGDATA *>(rtl_allocateString(
-            sizeof (IMPL_RTL_STRINGDATA) + nLen * sizeof 
(STRCODE<IMPL_RTL_STRINGDATA>)))
+            fix + nLen * sizeof (STRCODE<IMPL_RTL_STRINGDATA>)))
         : nullptr;
     if (pData != nullptr) {
         pData->refCount = 1;

Reply via email to