summaryrefslogtreecommitdiff
path: root/sal
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2021-10-01 08:14:29 +0200
committerMike Kaganski <mike.kaganski@collabora.com>2021-10-01 13:46:32 +0200
commiteae24a9488814e77254d175c11fc4a138c1dbd30 (patch)
treeabd87892b18e796377c4eadfcf2ecd2d7503fece /sal
parentab8a67e8f77269d6cdaffa67335ae21c0a3dc942 (diff)
Always use buffer on stack
Change-Id: I39ed2485a67ec7a8b24ab90ea0d69a5982374334 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122860 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'sal')
-rw-r--r--sal/rtl/math.cxx24
1 files changed, 5 insertions, 19 deletions
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<typename T::Char*>(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);
}
}