cid#1659971 silence Overflowed return value

and

cid#1659699 Overflowed return value
cid#1659759 Overflowed return value
cid#1659922 Overflowed return value
cid#1660190 Overflowed return value
cid#1660286 Overflowed return value
cid#1660457 Overflowed return value

Change-Id: Ie9fffe4994b10ed3d4feb14eaf7950e3f01714ef
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/189927
Reviewed-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Tested-by: Jenkins
This commit is contained in:
Caolán McNamara
2025-08-20 15:08:44 +02:00
parent 5cdefeef2a
commit be6015e3c4
25 changed files with 115 additions and 68 deletions
@@ -22,6 +22,7 @@
#include <cmath>
#include <limits>
#include <o3tl/untaint.hxx>
#include <rtl/math.hxx>
#include <rtl/ustrbuf.hxx>
@@ -36,6 +36,7 @@
#include <sal/log.hxx>
#include <osl/diagnose.h>
#include <comphelper/diagnose_ex.hxx>
#include <o3tl/untaint.hxx>
#include <tools/helpers.hxx>
#include <limits>
@@ -24,7 +24,6 @@
#if defined LIBO_USE_SOURCE_LOCATION
// expected-error@o3tl/runtimetooustring.hxx:* {{read s [loplugin:writeonlyvars]}}
// expected-error@o3tl/runtimetooustring.hxx:* {{write s [loplugin:writeonlyvars]}}
// expected-error@o3tl/safeint.hxx:* {{read res [loplugin:writeonlyvars]}}
#if !defined NDEBUG
// expected-error@o3tl/runtimetooustring.hxx:* {{read ok [loplugin:writeonlyvars]}}
#endif
@@ -18,6 +18,7 @@
*/
#include <dbase/DIndexIter.hxx>
#include <o3tl/untaint.hxx>
#include <com/sun/star/sdb/SQLFilterOperator.hpp>
using namespace ::com::sun::star::sdb;
@@ -19,7 +19,7 @@
#include <dbase/dindexnode.hxx>
#include <dbase/DIndex.hxx>
#include <o3tl/safeint.hxx>
#include <o3tl/untaint.hxx>
#include <tools/debug.hxx>
#include <tools/stream.hxx>
#include <sal/log.hxx>
-56
View File
@@ -227,62 +227,6 @@ clamp_to_unsigned(T2 value) {
// tools like -fsanitize=implicit-conversion should still be able to detect truncation:
template<typename T1, typename T2> constexpr T1 narrowing(T2 value) { return value; }
// std::min wrapped to inform coverity that the result is now deemed sanitized
// coverity[ -taint_source ]
template<typename T> [[nodiscard]] inline T sanitizing_min(T a, T b)
{
return std::min(a, b);
}
// For use when std::inf is an acceptable result
[[nodiscard]]
#if defined __clang__ || defined __GNUC__
__attribute__((no_sanitize("float-divide-by-zero")))
#endif
inline double div_allow_zero(double a, double b)
{
#if defined(__COVERITY__) && __COVERITY_MAJOR__ <= 2024
assert(b != 0 && "suppress floating point divide_by_zero");
#endif
return a / b;
}
// To sanitize in/de-crementing value where the result is known by the caller to be guaranteed to fit in
// the source type range without over/under-flow
[[nodiscard]] inline unsigned short sanitizing_inc(unsigned short value)
{
int res = value + 1;
assert(res <= std::numeric_limits<unsigned short>::max() &&
"nValue was supposed to be incrementable without overflow");
return static_cast<unsigned short>(res);
}
[[nodiscard]] inline unsigned short sanitizing_dec(unsigned short value)
{
int res = value - 1;
assert(res >= 0 &&
"nValue was supposed to be decrementable without underflow");
return static_cast<unsigned short>(res);
}
[[nodiscard]] inline short sanitizing_inc(short value)
{
int res = value + 1;
assert(res >= std::numeric_limits<short>::min() &&
res <= std::numeric_limits<short>::max() &&
"nValue was supposed to be incrementable without overflow");
return static_cast<short>(res);
}
[[nodiscard]] inline short sanitizing_dec(short value)
{
int res = value - 1;
assert(res >= std::numeric_limits<short>::min() &&
res <= std::numeric_limits<short>::max() &&
"nValue was supposed to be decrementable without underflow");
return static_cast<short>(res);
}
// A helper for taking care of signed/unsigned comparisons in constant bounds case
// Should avoid Coverity warnings like "cid#1618764 Operands don't affect result"
template <typename I, I Min = std::template numeric_limits<I>::min(),
+81
View File
@@ -0,0 +1,81 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <sal/config.h>
#include <algorithm>
#include <cassert>
#include <limits>
namespace o3tl
{
// For use when std::inf is an acceptable result
[[nodiscard]]
#if defined __clang__ || defined __GNUC__
__attribute__((no_sanitize("float-divide-by-zero")))
#endif
inline double
div_allow_zero(double a, double b)
{
#if defined(__COVERITY__) && __COVERITY_MAJOR__ <= 2024
assert(b != 0 && "suppress floating point divide_by_zero");
#endif
return a / b;
}
// std::min wrapped to inform coverity that the result is now deemed sanitized
// coverity[ -taint_source ]
template <typename T>[[nodiscard]] inline T sanitizing_min(T a, T b) { return std::min(a, b); }
// To sanitize in/de-crementing value where the result is known by the caller to be guaranteed to fit in
// the source type range without over/under-flow
[[nodiscard]] inline unsigned short sanitizing_inc(unsigned short value)
{
int res = value + 1;
assert(res <= std::numeric_limits<unsigned short>::max()
&& "nValue was supposed to be incrementable without overflow");
return static_cast<unsigned short>(res);
}
[[nodiscard]] inline unsigned short sanitizing_dec(unsigned short value)
{
int res = value - 1;
assert(res >= 0 && "nValue was supposed to be decrementable without underflow");
return static_cast<unsigned short>(res);
}
[[nodiscard]] inline short sanitizing_inc(short value)
{
int res = value + 1;
assert(res >= std::numeric_limits<short>::min() && res <= std::numeric_limits<short>::max()
&& "nValue was supposed to be incrementable without overflow");
return static_cast<short>(res);
}
[[nodiscard]] inline short sanitizing_dec(short value)
{
int res = value - 1;
assert(res >= std::numeric_limits<short>::min() && res <= std::numeric_limits<short>::max()
&& "nValue was supposed to be decrementable without underflow");
return static_cast<short>(res);
}
// A hammer that can be used when coverity refuses to accept that code is safe
// from overflow.
template <typename T> inline void untaint_for_overrun([[maybe_unused]] T& a)
{
#if defined(__COVERITY__) && __COVERITY_MAJOR__ <= 2024
__coverity_mark_pointee_as_sanitized__(&a, OVERRUN);
#endif
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
@@ -80,6 +80,7 @@
#include <xfilter/xfnumberstyle.hxx>
#include <xfilter/xfparastyle.hxx>
#include <o3tl/sorted_vector.hxx>
#include <o3tl/untaint.hxx>
#include <sal/log.hxx>
#include <comphelper/configuration.hxx>
+7 -2
View File
@@ -22,6 +22,7 @@
#include <cassert>
#include <cstdlib>
#include <o3tl/untaint.hxx>
#include <osl/diagnose.h>
#include <rtl/tencinfo.h>
@@ -380,7 +381,9 @@ sal_Int32 SAL_CALL rtl_str_indexOfChar(const char* pStr, char c) noexcept
sal_Int32 SAL_CALL rtl_str_indexOfChar_WithLength(const char* pStr, sal_Int32 nLen, char c) noexcept
{
return rtl::str::indexOfChar_WithLength(pStr, nLen, c);
sal_Int32 ret = rtl::str::indexOfChar_WithLength(pStr, nLen, c);
o3tl::untaint_for_overrun(ret);
return ret;
}
sal_Int32 SAL_CALL rtl_str_lastIndexOfChar(const char* pStr, char c) noexcept
@@ -401,7 +404,9 @@ sal_Int32 SAL_CALL rtl_str_indexOfStr(const char* pStr, const char* pSubStr) noe
sal_Int32 SAL_CALL rtl_str_indexOfStr_WithLength(const char* pStr, sal_Int32 nStrLen,
const char* pSubStr, sal_Int32 nSubLen) noexcept
{
return rtl::str::indexOfStr_WithLength(pStr, nStrLen, pSubStr, nSubLen);
sal_Int32 ret = rtl::str::indexOfStr_WithLength(pStr, nStrLen, pSubStr, nSubLen);
o3tl::untaint_for_overrun(ret);
return ret;
}
sal_Int32 SAL_CALL rtl_str_lastIndexOfStr(const char* pStr, const char* pSubStr) noexcept
+7 -2
View File
@@ -26,6 +26,7 @@
#include <config_options.h>
#include <o3tl/string_view.hxx>
#include <o3tl/untaint.hxx>
#include <osl/diagnose.h>
#include <osl/interlck.h>
#include <osl/mutex.h>
@@ -966,7 +967,9 @@ sal_Int32 SAL_CALL rtl_ustr_indexOfChar(const sal_Unicode* pStr, sal_Unicode c)
sal_Int32 SAL_CALL rtl_ustr_indexOfChar_WithLength(const sal_Unicode* pStr, sal_Int32 nLen,
sal_Unicode c) noexcept
{
return rtl::str::indexOfChar_WithLength(pStr, nLen, c);
sal_Int32 nRet = rtl::str::indexOfChar_WithLength(pStr, nLen, c);
o3tl::untaint_for_overrun(nRet);
return nRet;
}
sal_Int32 SAL_CALL rtl_ustr_lastIndexOfChar(const sal_Unicode* pStr, sal_Unicode c) noexcept
@@ -988,7 +991,9 @@ sal_Int32 SAL_CALL rtl_ustr_indexOfStr(const sal_Unicode* pStr, const sal_Unicod
sal_Int32 SAL_CALL rtl_ustr_indexOfStr_WithLength(const sal_Unicode* pStr, sal_Int32 nStrLen,
const sal_Unicode* pSubStr, sal_Int32 nSubLen) noexcept
{
return rtl::str::indexOfStr_WithLength(pStr, nStrLen, pSubStr, nSubLen);
sal_Int32 nRet = rtl::str::indexOfStr_WithLength(pStr, nStrLen, pSubStr, nSubLen);
o3tl::untaint_for_overrun(nRet);
return nRet;
}
sal_Int32 SAL_CALL rtl_ustr_lastIndexOfStr(const sal_Unicode* pStr, const sal_Unicode* pSubStr) noexcept
+1
View File
@@ -9,6 +9,7 @@
#pragma once
#include <o3tl/untaint.hxx>
#include <rtl/math.hxx>
#include <cmath>
+1
View File
@@ -21,6 +21,7 @@
#include <editeng/boxitem.hxx>
#include <editeng/editobj.hxx>
#include <o3tl/untaint.hxx>
#include <svx/svditer.hxx>
#include <sfx2/docfile.hxx>
#include <svl/numformat.hxx>
+1
View File
@@ -18,6 +18,7 @@
*/
#include <subtotal.hxx>
#include <o3tl/untaint.hxx>
#include <sal/mathconf.h>
#include <cfloat>
+1 -1
View File
@@ -24,7 +24,7 @@
#include <scitems.hxx>
#include <o3tl/safeint.hxx>
#include <o3tl/untaint.hxx>
#include <sfx2/docfile.hxx>
#include <svx/svxids.hrc>
#include <svl/numformat.hxx>
+1 -1
View File
@@ -19,7 +19,7 @@
#include <comphelper/docpasswordhelper.hxx>
#include <comphelper/sequenceashashmap.hxx>
#include <o3tl/safeint.hxx>
#include <o3tl/untaint.hxx>
#include <osl/thread.h>
#include <osl/diagnose.h>
#include <sal/log.hxx>
+1
View File
@@ -26,6 +26,7 @@
#include <xlroot.hxx>
#include <comphelper/string.hxx>
#include <o3tl/untaint.hxx>
#include <svl/sharedstringpool.hxx>
using namespace ::formula;
+1 -1
View File
@@ -8,7 +8,7 @@
*/
#include "xltoolbar.hxx"
#include <sal/log.hxx>
#include <o3tl/safeint.hxx>
#include <o3tl/untaint.hxx>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
#include <com/sun/star/ui/theModuleUIConfigurationManagerSupplier.hpp>
@@ -24,6 +24,7 @@
#include <string.h>
#include <stdio.h>
#include <o3tl/any.hxx>
#include <o3tl/untaint.hxx>
#include <rtl/math.hxx>
#include <algorithm>
#include <cmath>
+1
View File
@@ -21,6 +21,7 @@
#include "analysis.hxx"
#include "analysishelper.hxx"
#include <o3tl/temporary.hxx>
#include <o3tl/untaint.hxx>
using namespace sca::analysis;
+1
View File
@@ -20,6 +20,7 @@
#include <stdlib.h>
#include <comphelper/string.hxx>
#include <o3tl/untaint.hxx>
#include <sal/log.hxx>
#include <tools/debug.hxx>
#include <i18nlangtag/mslangid.hxx>
@@ -19,7 +19,7 @@
#include <sal/config.h>
#include <o3tl/safeint.hxx>
#include <o3tl/untaint.hxx>
#include <svx/EnhancedCustomShape2d.hxx>
#include <rtl/ustring.hxx>
#include <sal/log.hxx>
+1
View File
@@ -60,6 +60,7 @@
#include <vector>
#include <calbck.hxx>
#include <o3tl/string_view.hxx>
#include <o3tl/untaint.hxx>
#include <svl/numformat.hxx>
#include <txtfld.hxx>
#include <rolbck.hxx>
+1 -1
View File
@@ -38,7 +38,7 @@
#include <comphelper/string.hxx>
#include <unotools/localedatawrapper.hxx>
#include <i18nlangtag/lang.h>
#include <o3tl/safeint.hxx>
#include <o3tl/untaint.hxx>
#include <tools/stream.hxx>
#include <vcl/settings.hxx>
+1 -1
View File
@@ -20,7 +20,7 @@
#include <sal/config.h>
#include <osl/diagnose.h>
#include <o3tl/safeint.hxx>
#include <o3tl/untaint.hxx>
#include <sal/log.hxx>
#include <svl/style.hxx>
#include <utility>
+1
View File
@@ -28,6 +28,7 @@
#include <string.h>
#include <o3tl/safeint.hxx>
#include <o3tl/untaint.hxx>
#include <osl/endian.h>
#include <osl/diagnose.h>
#include <rtl/strbuf.hxx>