diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2020-01-27 09:30:39 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2020-01-28 07:42:15 +0100 |
commit | aef7feb3e695ecf6d411f0777196dcc4281e201a (patch) | |
tree | 6adff7e08e6431ff87c575d026e330badb9a6cd3 /reportdesign/source | |
parent | 65f007c629e5a7b2710e21e3f26164b433576e27 (diff) |
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<sal_uInt32>(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 <sbergman@redhat.com>
Diffstat (limited to 'reportdesign/source')
-rw-r--r-- | reportdesign/source/filter/xml/xmlTable.cxx | 7 | ||||
-rw-r--r-- | reportdesign/source/ui/dlg/CondFormat.cxx | 4 | ||||
-rw-r--r-- | reportdesign/source/ui/inspection/GeometryHandler.cxx | 3 | ||||
-rw-r--r-- | reportdesign/source/ui/misc/FunctionHelper.cxx | 7 |
4 files changed, 12 insertions, 9 deletions
diff --git a/reportdesign/source/filter/xml/xmlTable.cxx b/reportdesign/source/filter/xml/xmlTable.cxx index 5c400a33512d..a960beaf442e 100644 --- a/reportdesign/source/filter/xml/xmlTable.cxx +++ b/reportdesign/source/filter/xml/xmlTable.cxx @@ -18,6 +18,7 @@ */ #include "xmlTable.hxx" #include "xmlfilter.hxx" +#include <o3tl/safeint.hxx> #include <xmloff/xmltoken.hxx> #include <xmloff/xmlnmspe.hxx> #include <xmloff/nmspmap.hxx> @@ -223,7 +224,7 @@ void OXMLTable::endFastElement(sal_Int32 ) { if ( xFixedLine->getOrientation() == 1 ) // vertical { - OSL_ENSURE(static_cast<sal_uInt32>(j+1) < m_aWidth.size(),"Illegal pos of col iter. There should be an empty cell for the next line part."); + OSL_ENSURE(o3tl::make_unsigned(j+1) < m_aWidth.size(),"Illegal pos of col iter. There should be an empty cell for the next line part."); nWidth += m_aWidth[j+1]; if ( nWidth < MIN_WIDTH ) nWidth = MIN_WIDTH; @@ -258,9 +259,9 @@ void OXMLTable::endFastElement(sal_Int32 ) void OXMLTable::addCell(const Reference<XReportComponent>& _xElement) { uno::Reference<report::XShape> xShape(_xElement,uno::UNO_QUERY); - OSL_ENSURE(static_cast<sal_uInt32>(m_nRowIndex-1 ) < m_aGrid.size() && static_cast<sal_uInt32>( m_nColumnIndex-1 ) < m_aGrid[m_nRowIndex-1].size(), + OSL_ENSURE(o3tl::make_unsigned(m_nRowIndex-1 ) < m_aGrid.size() && o3tl::make_unsigned( m_nColumnIndex-1 ) < m_aGrid[m_nRowIndex-1].size(), "OXMLTable::addCell: Invalid column and row index"); - if ( static_cast<sal_uInt32>(m_nRowIndex-1 ) < m_aGrid.size() && static_cast<sal_uInt32>( m_nColumnIndex-1 ) < m_aGrid[m_nRowIndex-1].size() ) + if ( o3tl::make_unsigned(m_nRowIndex-1 ) < m_aGrid.size() && o3tl::make_unsigned( m_nColumnIndex-1 ) < m_aGrid[m_nRowIndex-1].size() ) { TCell& rCell = m_aGrid[m_nRowIndex-1][m_nColumnIndex-1]; if ( _xElement.is() ) diff --git a/reportdesign/source/ui/dlg/CondFormat.cxx b/reportdesign/source/ui/dlg/CondFormat.cxx index 016aa868cbae..c7a64b297d46 100644 --- a/reportdesign/source/ui/dlg/CondFormat.cxx +++ b/reportdesign/source/ui/dlg/CondFormat.cxx @@ -29,7 +29,7 @@ #include <com/sun/star/beans/XPropertySet.hpp> #include <com/sun/star/lang/IllegalArgumentException.hpp> - +#include <o3tl/safeint.hxx> #include <toolkit/helper/vclunohelper.hxx> #include <vcl/settings.hxx> @@ -145,7 +145,7 @@ namespace rptui { try { - if ( _nNewCondIndex > static_cast<size_t>(m_xCopy->getCount()) ) + if ( _nNewCondIndex > o3tl::make_unsigned(m_xCopy->getCount()) ) throw IllegalArgumentException(); Reference< XFormatCondition > xCond = m_xCopy->createFormatCondition(); diff --git a/reportdesign/source/ui/inspection/GeometryHandler.cxx b/reportdesign/source/ui/inspection/GeometryHandler.cxx index fb7bdb9e503c..34458e378756 100644 --- a/reportdesign/source/ui/inspection/GeometryHandler.cxx +++ b/reportdesign/source/ui/inspection/GeometryHandler.cxx @@ -106,6 +106,7 @@ #include <helpids.h> #include <toolkit/helper/convert.hxx> #include <o3tl/functional.hxx> +#include <o3tl/safeint.hxx> #define DATA_OR_FORMULA 0 #define FUNCTION 1 @@ -1257,7 +1258,7 @@ uno::Any SAL_CALL GeometryHandler::convertToControlValue(const OUString & Proper { sal_Int16 nParagraphAdjust = sal_Int16(style::ParagraphAdjust_LEFT); aPropertyValue >>= nParagraphAdjust; - if (static_cast<sal_uInt32>(nParagraphAdjust) < SAL_N_ELEMENTS(RID_STR_PARAADJUST_CONST) - 1) + if (o3tl::make_unsigned(nParagraphAdjust) < SAL_N_ELEMENTS(RID_STR_PARAADJUST_CONST) - 1) aControlValue <<= RptResId(RID_STR_PARAADJUST_CONST[nParagraphAdjust]); } break; diff --git a/reportdesign/source/ui/misc/FunctionHelper.cxx b/reportdesign/source/ui/misc/FunctionHelper.cxx index f42e65cc204b..00043d4a94fd 100644 --- a/reportdesign/source/ui/misc/FunctionHelper.cxx +++ b/reportdesign/source/ui/misc/FunctionHelper.cxx @@ -19,6 +19,7 @@ #include <FunctionHelper.hxx> +#include <o3tl/safeint.hxx> #include <osl/diagnose.h> #include <formula/funcvarargs.h> @@ -236,21 +237,21 @@ sal_uInt32 FunctionDescription::getVarArgsStart() const OUString FunctionDescription::getParameterName(sal_uInt32 _nPos) const { - if ( _nPos < static_cast<sal_uInt32>(m_aParameter.getLength()) ) + if ( _nPos < o3tl::make_unsigned(m_aParameter.getLength()) ) return m_aParameter[_nPos].Name; return OUString(); } OUString FunctionDescription::getParameterDescription(sal_uInt32 _nPos) const { - if ( _nPos < static_cast<sal_uInt32>(m_aParameter.getLength()) ) + if ( _nPos < o3tl::make_unsigned(m_aParameter.getLength()) ) return m_aParameter[_nPos].Description; return OUString(); } bool FunctionDescription::isParameterOptional(sal_uInt32 _nPos) const { - if ( _nPos < static_cast<sal_uInt32>(m_aParameter.getLength()) ) + if ( _nPos < o3tl::make_unsigned(m_aParameter.getLength()) ) return m_aParameter[_nPos].IsOptional; return false; } |