Kaydet (Commit) bd60d411 authored tarafından Zolnai Tamás's avatar Zolnai Tamás Kaydeden (comit) Stephan Bergmann

Handle oveflow in O(U)String::toInt() functions

Return 0 when overflow.
The base idea in unsigned case is checking wheather
(Max-nDigit)/nRadix < n
But for efficency, take out nDiv = Max/nRadix from loop
and corrigate it with -1 if needed.
In signed case use minimum value if the number is negativ.

Change-Id: I5b77580adbf12421b6c4b785ba9bc2a080accba2
Signed-off-by: 's avatarStephan Bergmann <sbergman@redhat.com>
üst 476081b1
......@@ -941,11 +941,15 @@ namespace {
bNeg = sal_False;
}
const T nDiv = bNeg ? -(std::numeric_limits<T>::min()/nRadix) : (std::numeric_limits<T>::max()/nRadix);
const sal_Int16 nMod = bNeg ? -(std::numeric_limits<T>::min()%nRadix) : (std::numeric_limits<T>::max()%nRadix);
while ( *pStr )
{
nDigit = rtl_ImplGetDigit( IMPL_RTL_USTRCODE( *pStr ), nRadix );
if ( nDigit < 0 )
break;
if( ( nMod < nDigit ? nDiv-1 : nDiv ) < n )
return 0;
n *= nRadix;
n += nDigit;
......@@ -978,11 +982,15 @@ namespace {
if ( *pStr == '+' )
++pStr;
const T nDiv = std::numeric_limits<T>::max()/nRadix;
const sal_Int16 nMod = std::numeric_limits<T>::max()%nRadix;
while ( *pStr )
{
nDigit = rtl_ImplGetDigit( IMPL_RTL_USTRCODE( *pStr ), nRadix );
if ( nDigit < 0 )
break;
if( ( nMod < nDigit ? nDiv-1 : nDiv ) < n )
return 0;
n *= nRadix;
n += nDigit;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment