From aef7feb3e695ecf6d411f0777196dcc4281e201a Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Mon, 27 Jan 2020 09:30:39 +0100 Subject: New loplugin:unsignedcompare "Find explicit casts from signed to unsigned integer in comparison against unsigned integer, where the cast is presumably used to avoid warnings about signed vs. unsigned comparisons, and could thus be replaced with o3tl::make_unsigned for clairty." (compilerplugins/clang/unsignedcompare.cxx) o3tl::make_unsigned requires its argument to be non-negative, and there is a chance that some original code like static_cast(n) >= c used the explicit cast to actually force a (potentially negative) value of sal_Int32 to be interpreted as an unsigned sal_uInt32, rather than using the cast to avoid a false "signed vs. unsigned comparison" warning in a case where n is known to be non-negative. It appears that restricting this plugin to non- equality comparisons (<, >, <=, >=) and excluding equality comparisons (==, !=) is a useful heuristic to avoid such false positives. The only remainging false positive I found was 0288c8ffecff4956a52b9147d441979941e8b87f "Rephrase cast from sal_Int32 to sal_uInt32". But which of course does not mean that there were no further false positivies that I missed. So this commit may accidentally introduce some false hits of the assert in o3tl::make_unsigned. At least, it passed a full (Linux ASan+UBSan --enable-dbgutil) `make check && make screenshot`. It is by design that o3tl::make_unsigned only accepts signed integer parameter types (and is not defined as a nop for unsigned ones), to avoid unnecessary uses which would in general be suspicious. But the STATIC_ARRAY_SELECT macro in include/oox/helper/helper.hxx is used with both signed and unsigned types, so needs a little oox::detail::make_unsigned helper function for now. (The ultimate fix being to get rid of the macro in the first place.) Change-Id: Ia4adc9f44c70ad1dfd608784cac39ee922c32175 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/87556 Tested-by: Jenkins Reviewed-by: Stephan Bergmann --- basic/source/runtime/runtime.cxx | 4 ++-- basic/source/sbx/sbxarray.cxx | 4 ++-- basic/source/sbx/sbxbyte.cxx | 5 ++++- basic/source/sbx/sbxint.cxx | 3 ++- 4 files changed, 10 insertions(+), 6 deletions(-) (limited to 'basic') diff --git a/basic/source/runtime/runtime.cxx b/basic/source/runtime/runtime.cxx index a663dbed961f..46879bb6a65e 100644 --- a/basic/source/runtime/runtime.cxx +++ b/basic/source/runtime/runtime.cxx @@ -32,7 +32,7 @@ #include #include - +#include #include #include @@ -2961,7 +2961,7 @@ void SbiRuntime::StepONJUMP( sal_uInt32 nOp1 ) nOp1 &= 0x7FFF; PushGosub( pCode + 5 * nOp1 ); } - if( n < 1 || static_cast(n) > nOp1 ) + if( n < 1 || o3tl::make_unsigned(n) > nOp1 ) n = static_cast( nOp1 + 1 ); nOp1 = static_cast( reinterpret_cast(pCode) - pImg->GetCode() ) + 5 * --n; StepJUMP( nOp1 ); diff --git a/basic/source/sbx/sbxarray.cxx b/basic/source/sbx/sbxarray.cxx index 152ab5a671f4..172311808836 100644 --- a/basic/source/sbx/sbxarray.cxx +++ b/basic/source/sbx/sbxarray.cxx @@ -18,7 +18,7 @@ */ #include - +#include #include #include #include @@ -518,7 +518,7 @@ sal_uInt32 SbxDimArray::Offset32( SbxArray* pPar ) if (IsError()) break; } - if( nPos > sal_uInt32(SBX_MAXINDEX32) ) + if( nPos > o3tl::make_unsigned(SBX_MAXINDEX32) ) { SetError( ERRCODE_BASIC_OUT_OF_RANGE ); nPos = 0; diff --git a/basic/source/sbx/sbxbyte.cxx b/basic/source/sbx/sbxbyte.cxx index 6557aa3fff4b..4974213dc7a1 100644 --- a/basic/source/sbx/sbxbyte.cxx +++ b/basic/source/sbx/sbxbyte.cxx @@ -17,6 +17,9 @@ * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ +#include + +#include #include //#include #include @@ -61,7 +64,7 @@ start: break; case SbxERROR: case SbxUSHORT: - if( p->nUShort > sal_uInt16(SbxMAXBYTE) ) + if( p->nUShort > o3tl::make_unsigned(SbxMAXBYTE) ) { SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXBYTE; } diff --git a/basic/source/sbx/sbxint.cxx b/basic/source/sbx/sbxint.cxx index 2ba76c89e352..a28b810eb8c7 100644 --- a/basic/source/sbx/sbxint.cxx +++ b/basic/source/sbx/sbxint.cxx @@ -20,6 +20,7 @@ #include #include +#include #include #include #include "sbxconv.hxx" @@ -47,7 +48,7 @@ start: nRes = p->nInteger; break; case SbxERROR: case SbxUSHORT: - if( p->nUShort > sal_uInt16(SbxMAXINT) ) + if( p->nUShort > o3tl::make_unsigned(SbxMAXINT) ) { SbxBase::SetError( ERRCODE_BASIC_MATH_OVERFLOW ); nRes = SbxMAXINT; } -- cgit