From eae24a9488814e77254d175c11fc4a138c1dbd30 Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Fri, 1 Oct 2021 08:14:29 +0200 Subject: Always use buffer on stack Change-Id: I39ed2485a67ec7a8b24ab90ea0d69a5982374334 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122860 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- sal/rtl/math.cxx | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) (limited to 'sal/rtl') diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx index 95dd6ae41bc6..7ad1671924ab 100644 --- a/sal/rtl/math.cxx +++ b/sal/rtl/math.cxx @@ -500,24 +500,13 @@ void doubleToString(typename T::String ** pResult, } } - static sal_Int32 const nBufMax = 256; - typename T::Char aBuf[nBufMax]; - typename T::Char * pBuf; sal_Int32 nBuf = static_cast< sal_Int32 > (nDigits <= 0 ? std::max< sal_Int32 >(nDecPlaces, abs(nExp)) : nDigits + nDecPlaces ) + 10 + (pGroups ? abs(nDigits) * 2 : 0); - - if (nBuf > nBufMax) - { - pBuf = static_cast< typename T::Char * >( - malloc(nBuf * sizeof (typename T::Char))); - OSL_ENSURE(pBuf, "Out of memory"); - } - else - { - pBuf = aBuf; - } - + // max(nDigits) = max(nDecPlaces) + 1 + max(nExp) + 1 = 20 + 1 + 308 + 1 = 330 + // max(nBuf) = max(nDigits) + max(nDecPlaces) + 10 + max(nDigits) * 2 = 330 * 3 + 20 + 10 = 1020 + assert(nBuf <= 1024); + typename T::Char* pBuf = static_cast(alloca(nBuf)); typename T::Char * p = pBuf; if ( bSign ) *p++ = static_cast< typename T::Char >('-'); @@ -595,7 +584,7 @@ void doubleToString(typename T::String ** pResult, if (sLen == -1 || (sLen == 0 && bSign)) { // Assert that no one changed the logic we rely on. - assert(!bSign || *pBuf == static_cast< typename T::Char >('-')); + assert(!bSign || pBuf[0] == static_cast< typename T::Char >('-')); p = pBuf; if (bSign) ++p; @@ -761,9 +750,6 @@ void doubleToString(typename T::String ** pResult, T::createString(pResult, pBuf, p - pBuf); else T::appendChars(pResult, pResultCapacity, &nResultOffset, pBuf, p - pBuf); - - if (pBuf != &aBuf[0]) - free(pBuf); } } -- cgit