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 /extensions | |
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 'extensions')
-rw-r--r-- | extensions/source/dbpilots/groupboxwiz.cxx | 5 | ||||
-rw-r--r-- | extensions/source/scanner/sane.cxx | 4 | ||||
-rw-r--r-- | extensions/source/scanner/scanunx.cxx | 9 |
3 files changed, 11 insertions, 7 deletions
diff --git a/extensions/source/dbpilots/groupboxwiz.cxx b/extensions/source/dbpilots/groupboxwiz.cxx index 97bf0273da38..4e723e1685e3 100644 --- a/extensions/source/dbpilots/groupboxwiz.cxx +++ b/extensions/source/dbpilots/groupboxwiz.cxx @@ -24,6 +24,7 @@ #include "optiongrouplayouter.hxx" #include <helpids.h> #include <comphelper/processfactory.hxx> +#include <o3tl/safeint.hxx> #include <osl/diagnose.h> #define GBW_STATE_OPTIONLIST 0 @@ -361,12 +362,12 @@ namespace dbp if (::vcl::WizardTypes::WizardState(-1) != m_nLastSelection) { // save the value for the last option - DBG_ASSERT(static_cast<size_t>(m_nLastSelection) < m_aUncommittedValues.size(), "OOptionValuesPage::implTraveledOptions: invalid previous selection index!"); + DBG_ASSERT(o3tl::make_unsigned(m_nLastSelection) < m_aUncommittedValues.size(), "OOptionValuesPage::implTraveledOptions: invalid previous selection index!"); m_aUncommittedValues[m_nLastSelection] = m_xValue->get_text(); } m_nLastSelection = m_xOptions->get_selected_index(); - DBG_ASSERT(static_cast<size_t>(m_nLastSelection) < m_aUncommittedValues.size(), "OOptionValuesPage::implTraveledOptions: invalid new selection index!"); + DBG_ASSERT(o3tl::make_unsigned(m_nLastSelection) < m_aUncommittedValues.size(), "OOptionValuesPage::implTraveledOptions: invalid new selection index!"); m_xValue->set_text(m_aUncommittedValues[m_nLastSelection]); } diff --git a/extensions/source/scanner/sane.cxx b/extensions/source/scanner/sane.cxx index 67889ce00416..c58cf1e15c7c 100644 --- a/extensions/source/scanner/sane.cxx +++ b/extensions/source/scanner/sane.cxx @@ -20,6 +20,8 @@ #include <cstdarg> #include <type_traits> #include <math.h> + +#include <o3tl/safeint.hxx> #include <osl/file.h> #include <sal/log.hxx> #include <tools/stream.hxx> @@ -270,7 +272,7 @@ void Sane::ReloadOptions() fprintf( stderr, "Error: sane driver returned %s while reading number of options !\n", p_strstatus( nStatus ) ); mnOptions = pOptions[ 0 ]; - if( static_cast<size_t>(pZero->size) > sizeof( SANE_Word ) ) + if( o3tl::make_unsigned(pZero->size) > sizeof( SANE_Word ) ) fprintf( stderr, "driver returned number of options with larger size than SANE_Word!!!\n" ); mppOptions.reset(new const SANE_Option_Descriptor*[ mnOptions ]); mppOptions[ 0 ] = pZero; diff --git a/extensions/source/scanner/scanunx.cxx b/extensions/source/scanner/scanunx.cxx index bd261d694c85..41c0b66dba24 100644 --- a/extensions/source/scanner/scanunx.cxx +++ b/extensions/source/scanner/scanunx.cxx @@ -19,6 +19,7 @@ #include "scanner.hxx" #include "sanedlg.hxx" +#include <o3tl/safeint.hxx> #include <osl/thread.hxx> #include <sal/log.hxx> #include <tools/solar.h> @@ -245,7 +246,7 @@ sal_Bool ScannerManager::configureScannerAndScan( ScannerContext& scanner_contex SAL_INFO("extensions.scanner", "ScannerManager::configureScanner"); - if( scanner_context.InternalData < 0 || static_cast<sal_uLong>(scanner_context.InternalData) >= rSanes.size() ) + if( scanner_context.InternalData < 0 || o3tl::make_unsigned(scanner_context.InternalData) >= rSanes.size() ) throw ScannerException( "Scanner does not exist", Reference< XScannerManager >( this ), @@ -281,7 +282,7 @@ void ScannerManager::startScan( const ScannerContext& scanner_context, SAL_INFO("extensions.scanner", "ScannerManager::startScan"); - if( scanner_context.InternalData < 0 || static_cast<sal_uLong>(scanner_context.InternalData) >= rSanes.size() ) + if( scanner_context.InternalData < 0 || o3tl::make_unsigned(scanner_context.InternalData) >= rSanes.size() ) throw ScannerException( "Scanner does not exist", Reference< XScannerManager >( this ), @@ -306,7 +307,7 @@ ScanError ScannerManager::getError( const ScannerContext& scanner_context ) osl::MutexGuard aGuard( theSaneProtector::get() ); sanevec &rSanes = theSanes::get().m_aSanes; - if( scanner_context.InternalData < 0 || static_cast<sal_uLong>(scanner_context.InternalData) >= rSanes.size() ) + if( scanner_context.InternalData < 0 || o3tl::make_unsigned(scanner_context.InternalData) >= rSanes.size() ) throw ScannerException( "Scanner does not exist", Reference< XScannerManager >( this ), @@ -324,7 +325,7 @@ Reference< css::awt::XBitmap > ScannerManager::getBitmap( const ScannerContext& osl::MutexGuard aGuard( theSaneProtector::get() ); sanevec &rSanes = theSanes::get().m_aSanes; - if( scanner_context.InternalData < 0 || static_cast<sal_uLong>(scanner_context.InternalData) >= rSanes.size() ) + if( scanner_context.InternalData < 0 || o3tl::make_unsigned(scanner_context.InternalData) >= rSanes.size() ) throw ScannerException( "Scanner does not exist", Reference< XScannerManager >( this ), |