diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-01-03 14:25:15 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-01-05 10:41:53 +0100 |
commit | adf0738d2dbfa742d0e9ef130954fb4638a8e90d (patch) | |
tree | 46798a23d0ddd43ec82563b52500ee8ed520857e /include | |
parent | 24a7df66149a907c2ca2e757927c6c88ecadc523 (diff) |
long->sal_Int32 in BigInt
And fix an issue discovered in the PPT parsing, where one of the
test files has dodgy values that trigger the assert in
BigInt::operator long().
Change-Id: Ic324ac38ecef4153cc434376317643ababe0a537
Reviewed-on: https://gerrit.libreoffice.org/47314
Tested-by: Jenkins <ci@libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'include')
-rw-r--r-- | include/tools/bigint.hxx | 119 |
1 files changed, 42 insertions, 77 deletions
diff --git a/include/tools/bigint.hxx b/include/tools/bigint.hxx index 849920c418a1..d94cdb06e946 100644 --- a/include/tools/bigint.hxx +++ b/include/tools/bigint.hxx @@ -33,8 +33,8 @@ class Fraction; class SAL_WARN_UNUSED TOOLS_DLLPUBLIC BigInt { private: - long nVal; - unsigned short nNum[MAX_DIGITS]; + sal_Int32 nVal; + sal_uInt16 nNum[MAX_DIGITS]; sal_uInt8 nLen : 5; // current length bool bIsNeg : 1, // Is Sign negative? bIsBig : 1, // sal_True == BigInt @@ -62,16 +62,7 @@ public: { } - BigInt(short nValue) - : nVal(nValue) - , nLen(0) - , bIsNeg(false) - , bIsBig(false) - , bIsSet(true) - { - } - - BigInt(long nValue) + BigInt(sal_Int32 nValue) : nVal(nValue) , nLen(0) , bIsNeg(false) @@ -80,6 +71,8 @@ public: { } + // despite sal_Int32 being a typedef for int, MSVC won't automatically use the BigInt(sal_Int32) constructor +#ifdef _WIN32 BigInt(int nValue) : nVal(nValue) , nLen(0) @@ -88,31 +81,22 @@ public: , bIsSet(true) { } +#endif BigInt( double nVal ); - - BigInt(sal_uInt16 nValue) - : nVal(nValue) - , nLen(0) - , bIsNeg(false) - , bIsBig(false) - , bIsSet(true) - { - } - BigInt( sal_uInt32 nVal ); -#if SAL_TYPES_SIZEOFLONG < SAL_TYPES_SIZEOFLONGLONG - BigInt( long long nVal ); -#endif + BigInt( sal_Int64 nVal ); BigInt( const BigInt& rBigInt ); BigInt( const OUString& rString ); - operator short() const; - operator long() const; - operator int() const; - operator double() const; + operator sal_Int16() const; operator sal_uInt16() const; - operator sal_uIntPtr() const; + operator sal_Int32() const; + operator sal_uInt32() const; + operator double() const; +#if SAL_TYPES_SIZEOFLONG == 8 + operator long() const; +#endif bool IsSet() const { return bIsSet; } bool IsNeg() const; @@ -128,10 +112,7 @@ public: BigInt& operator /=( const BigInt& rVal ); BigInt& operator %=( const BigInt& rVal ); - BigInt& operator =( const short nValue ); - BigInt& operator =( const long nValue ); - BigInt& operator =( const int nValue ); - BigInt& operator =( const sal_uInt16 nValue ); + BigInt& operator =( sal_Int32 nValue ); friend inline BigInt operator +( const BigInt& rVal1, const BigInt& rVal2 ); friend inline BigInt operator -( const BigInt& rVal1, const BigInt& rVal2 ); @@ -149,66 +130,50 @@ public: friend class Fraction; }; -inline BigInt::operator short() const +inline BigInt::operator sal_Int16() const { - if ( !bIsBig && nVal >= SHRT_MIN && nVal <= SHRT_MAX ) - return (short)nVal; - else - return 0; -} - -inline BigInt::operator long() const -{ - if ( !bIsBig ) - return nVal; - else - return 0; -} - -inline BigInt::operator int() const -{ - if ( !bIsBig && (nVal == (long)(int)nVal) ) - return (int)nVal; - else - return 0; + if ( !bIsBig && nVal >= SAL_MIN_INT16 && nVal <= SAL_MAX_INT16 ) + return (sal_Int16)nVal; + assert(false && "out of range"); + return 0; } inline BigInt::operator sal_uInt16() const { - if ( !bIsBig && nVal >= 0 && nVal <= (long)USHRT_MAX ) + if ( !bIsBig && nVal >= 0 && nVal <= SAL_MAX_UINT16 ) return (sal_uInt16)nVal; - else - return 0; + assert(false && "out of range"); + return 0; } -inline BigInt& BigInt::operator =( const short nValue ) +inline BigInt::operator sal_Int32() const { - bIsSet = true; - bIsBig = false; - nVal = nValue; - - return *this; + if ( !bIsBig && nVal >= SAL_MIN_INT32 && nVal <= SAL_MAX_INT32 ) + return (sal_Int32)nVal; + assert(false && "out of range"); + return 0; } -inline BigInt& BigInt::operator =( const long nValue ) +inline BigInt::operator sal_uInt32() const { - bIsSet = true; - bIsBig = false; - nVal = nValue; - - return *this; + if ( !bIsBig && nVal >= 0 ) + return (sal_uInt32)nVal; + assert(false && "out of range"); + return 0; } -inline BigInt& BigInt::operator =( const int nValue ) +#if SAL_TYPES_SIZEOFLONG == 8 +inline BigInt::operator long() const { - bIsSet = true; - bIsBig = false; - nVal = nValue; - - return *this; + // Clamp to int32 since long is int32 on Windows. + if ( !bIsBig && nVal >= SAL_MIN_INT32 && nVal <= SAL_MAX_INT32 ) + return (long)nVal; + assert(false && "out of range"); + return 0; } +#endif -inline BigInt& BigInt::operator =( const sal_uInt16 nValue ) +inline BigInt& BigInt::operator =( sal_Int32 nValue ) { bIsSet = true; bIsBig = false; |