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 /tools | |
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 'tools')
-rw-r--r-- | tools/source/stream/stream.cxx | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/tools/source/stream/stream.cxx b/tools/source/stream/stream.cxx index 8c38d862b3df..404b9eb67537 100644 --- a/tools/source/stream/stream.cxx +++ b/tools/source/stream/stream.cxx @@ -28,6 +28,7 @@ #include <string.h> #include <stdio.h> +#include <o3tl/safeint.hxx> #include <osl/endian.h> #include <osl/diagnose.h> #include <rtl/strbuf.hxx> @@ -488,7 +489,7 @@ bool SvStream::ReadLine( OString& rStr, sal_Int32 nMaxBytesToRead ) ++n; } nTotalLen += j; - if (nTotalLen > static_cast<std::size_t>(nMaxBytesToRead)) + if (nTotalLen > o3tl::make_unsigned(nMaxBytesToRead)) { n -= nTotalLen - nMaxBytesToRead; nTotalLen = nMaxBytesToRead; @@ -574,7 +575,7 @@ bool SvStream::ReadUniStringLine( OUString& rStr, sal_Int32 nMaxCodepointsToRead } } nTotalLen += j; - if (nTotalLen > static_cast<std::size_t>(nMaxCodepointsToRead)) + if (nTotalLen > o3tl::make_unsigned(nMaxCodepointsToRead)) { n -= nTotalLen - nMaxCodepointsToRead; nTotalLen = nMaxCodepointsToRead; @@ -796,7 +797,7 @@ sal_uInt64 SvStream::SeekRel(sal_Int64 const nPos) if ( nPos >= 0 ) { - if (SAL_MAX_UINT64 - nActualPos > static_cast<sal_uInt64>(nPos)) + if (SAL_MAX_UINT64 - nActualPos > o3tl::make_unsigned(nPos)) nActualPos += nPos; } else @@ -1244,7 +1245,7 @@ std::size_t SvStream::ReadBytes( void* pData, std::size_t nCount ) // check if block is completely within buffer m_isIoRead = true; m_isIoWrite = false; - if (nCount <= static_cast<std::size_t>(m_nBufActualLen - m_nBufActualPos)) + if (nCount <= o3tl::make_unsigned(m_nBufActualLen - m_nBufActualPos)) { // => yes if (nCount != 0) @@ -1329,7 +1330,7 @@ std::size_t SvStream::WriteBytes( const void* pData, std::size_t nCount ) m_isIoRead = false; m_isIoWrite = true; - if (nCount <= static_cast<std::size_t>(m_nBufSize - m_nBufActualPos)) + if (nCount <= o3tl::make_unsigned(m_nBufSize - m_nBufActualPos)) { memcpy( m_pBufPos, pData, nCount ); m_nBufActualPos = m_nBufActualPos + static_cast<sal_uInt16>(nCount); |