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 /sw/source/ui | |
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 'sw/source/ui')
-rw-r--r-- | sw/source/ui/dbui/mmaddressblockpage.cxx | 3 | ||||
-rw-r--r-- | sw/source/ui/fldui/fldref.cxx | 3 | ||||
-rw-r--r-- | sw/source/ui/frmdlg/frmpage.cxx | 3 | ||||
-rw-r--r-- | sw/source/ui/index/cnttab.cxx | 5 |
4 files changed, 10 insertions, 4 deletions
diff --git a/sw/source/ui/dbui/mmaddressblockpage.cxx b/sw/source/ui/dbui/mmaddressblockpage.cxx index a8545852032e..11d884ec6652 100644 --- a/sw/source/ui/dbui/mmaddressblockpage.cxx +++ b/sw/source/ui/dbui/mmaddressblockpage.cxx @@ -22,6 +22,7 @@ #include <swtypes.hxx> #include "addresslistdialog.hxx" #include <editeng/eeitem.hxx> +#include <o3tl/safeint.hxx> #include <svl/grabbagitem.hxx> #include <svl/itemset.hxx> #include <vcl/event.hxx> @@ -864,7 +865,7 @@ void SwAssignFieldsControl::Init(SwAssignFieldsDialog* pDialog, SwMailMergeConfi rNewLB.append_text(rField); //select the ListBox //if there is an assignment - if(static_cast<sal_uInt32>(aAssignments.getLength()) > i && !aAssignments[i].isEmpty()) + if(o3tl::make_unsigned(aAssignments.getLength()) > i && !aAssignments[i].isEmpty()) rNewLB.set_active_text(aAssignments[i]); else //otherwise the current column name may match one of the db columns rNewLB.set_active_text(rHeader); diff --git a/sw/source/ui/fldui/fldref.cxx b/sw/source/ui/fldui/fldref.cxx index a64f2eb20831..9a94726ad74d 100644 --- a/sw/source/ui/fldui/fldref.cxx +++ b/sw/source/ui/fldui/fldref.cxx @@ -33,6 +33,7 @@ #include <unotools/charclass.hxx> #include <comphelper/string.hxx> +#include <o3tl/safeint.hxx> #define REFFLDFLAG 0x4000 #define REFFLDFLAG_BOOKMARK 0x4800 @@ -191,7 +192,7 @@ void SwFieldRefPage::Reset(const SfxItemSet* ) const size_t nFieldTypeCnt = pSh->GetFieldTypeCount(SwFieldIds::SetExp); - OSL_ENSURE( nFieldTypeCnt < static_cast<size_t>(REFFLDFLAG), "<SwFieldRefPage::Reset> - Item index will overlap flags!" ); + OSL_ENSURE( nFieldTypeCnt < o3tl::make_unsigned(REFFLDFLAG), "<SwFieldRefPage::Reset> - Item index will overlap flags!" ); for (size_t n = 0; n < nFieldTypeCnt; ++n) { diff --git a/sw/source/ui/frmdlg/frmpage.cxx b/sw/source/ui/frmdlg/frmpage.cxx index 5ba7541fb468..00188e194f9a 100644 --- a/sw/source/ui/frmdlg/frmpage.cxx +++ b/sw/source/ui/frmdlg/frmpage.cxx @@ -24,6 +24,7 @@ #include <cmdid.h> #include <hintids.hxx> #include <bitmaps.hlst> +#include <o3tl/safeint.hxx> #include <vcl/mnemonic.hxx> #include <svl/stritem.hxx> #include <sfx2/htmlmode.hxx> @@ -1595,7 +1596,7 @@ sal_Int16 SwFramePage::GetAlignment(FrameMap const *pMap, sal_Int32 nMapPos, const size_t nMapCount = ::lcl_GetFrameMapCount(pMap); - if (static_cast<size_t>(nMapPos) >= nMapCount) + if (o3tl::make_unsigned(nMapPos) >= nMapCount) return 0; // i#22341 special handling also for map <aVCharMap>, diff --git a/sw/source/ui/index/cnttab.cxx b/sw/source/ui/index/cnttab.cxx index 0c181975d5bf..d5aa63eeeae6 100644 --- a/sw/source/ui/index/cnttab.cxx +++ b/sw/source/ui/index/cnttab.cxx @@ -17,6 +17,9 @@ * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ +#include <sal/config.h> + +#include <o3tl/safeint.hxx> #include <sal/log.hxx> #include <svl/style.hxx> #include <vcl/weld.hxx> @@ -3678,7 +3681,7 @@ bool SwEntryBrowseBox::SeekRow( long nRow ) OUString SwEntryBrowseBox::GetCellText(long nRow, sal_uInt16 nColumn) const { OUString pRet; - if (static_cast<size_t>(nRow) < m_Entries.size()) + if (o3tl::make_unsigned(nRow) < m_Entries.size()) { const AutoMarkEntry* pEntry = m_Entries[ nRow ].get(); switch(nColumn) |