diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2017-10-27 16:06:07 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2017-10-27 16:08:45 +0200 |
commit | 8959043f82e1d00abead9cbd809138d4f6521b14 (patch) | |
tree | 84e6c69b8b91be2db43f5f5d7040ab1c6ba6809a | |
parent | c1d0b19b43767e3ead431357e91949e1f548283e (diff) |
-Werror,-Wtautological-constant-compare (clang-cl)
...fixed similarly to recent fixes to sal/osl/unx/file.cxx
Change-Id: I2c82366095e156cd0085a8f60f54f8c822655dcc
-rw-r--r-- | sal/osl/w32/backtrace.cxx | 19 | ||||
-rw-r--r-- | sal/osl/w32/file.cxx | 42 |
2 files changed, 41 insertions, 20 deletions
diff --git a/sal/osl/w32/backtrace.cxx b/sal/osl/w32/backtrace.cxx index b8866322e940..408bd655e777 100644 --- a/sal/osl/w32/backtrace.cxx +++ b/sal/osl/w32/backtrace.cxx @@ -26,13 +26,19 @@ #include "backtraceasstring.hxx" +namespace { + +template<typename T> T clampToULONG(T n) { + auto const maxUlong = std::numeric_limits<ULONG>::max(); + return n > maxUlong ? static_cast<T>(maxUlong) : n; +} + +} + OUString osl::detail::backtraceAsString(sal_uInt32 maxDepth) { assert(maxDepth != 0); - auto const maxUlong = std::numeric_limits<ULONG>::max(); - if (maxDepth > maxUlong) { - maxDepth = static_cast<sal_uInt32>(maxUlong); - } + maxDepth = clampToULONG(maxDepth); OUStringBuffer aBuf; @@ -71,10 +77,7 @@ OUString osl::detail::backtraceAsString(sal_uInt32 maxDepth) std::unique_ptr<sal::BacktraceState> sal::backtrace_get(sal_uInt32 maxDepth) { assert(maxDepth != 0); - auto const maxUlong = std::numeric_limits<ULONG>::max(); - if (maxDepth > maxUlong) { - maxDepth = static_cast<sal_uInt32>(maxUlong); - } + maxDepth = clampToULONG(maxDepth); HANDLE hProcess = GetCurrentProcess(); SymInitialize( hProcess, nullptr, true ); diff --git a/sal/osl/w32/file.cxx b/sal/osl/w32/file.cxx index e64e5b306277..7c0b6674c23f 100644 --- a/sal/osl/w32/file.cxx +++ b/sal/osl/w32/file.cxx @@ -774,6 +774,14 @@ oslFileError SAL_CALL osl_closeFile(oslFileHandle Handle) return result; } +namespace { + +//coverity[result_independent_of_operands] +template<typename T> bool exceedsMaxSIZE_T(T n) +{ return n > std::numeric_limits< SIZE_T >::max(); } + +} + oslFileError SAL_CALL osl_mapFile( oslFileHandle Handle, void** ppAddr, @@ -800,8 +808,7 @@ oslFileError SAL_CALL osl_mapFile( return osl_File_E_INVAL; *ppAddr = nullptr; - static SIZE_T const nLimit = std::numeric_limits< SIZE_T >::max(); - if (uLength > nLimit) + if (exceedsMaxSIZE_T(uLength)) return osl_File_E_OVERFLOW; SIZE_T const nLength = sal::static_int_cast< SIZE_T >(uLength); @@ -919,6 +926,19 @@ oslFileError SAL_CALL osl_writeFile( return result; } +LONGLONG const g_limit_longlong = std::numeric_limits< LONGLONG >::max(); + +namespace { + +//coverity[result_independent_of_operands] +template<typename T> bool exceedsMaxLONGLONG(T n) +{ return n > g_limit_longlong; } + +template<typename T> bool exceedsMinLONGLONG(T n) +{ return n < std::numeric_limits<LONGLONG>::min(); } + +} + oslFileError SAL_CALL osl_readFileAt( oslFileHandle Handle, sal_uInt64 uOffset, @@ -933,8 +953,7 @@ oslFileError SAL_CALL osl_readFileAt( if ((pImpl->m_state & FileHandle_Impl::STATE_SEEKABLE) == 0) return osl_File_E_SPIPE; - static sal_uInt64 const g_limit_longlong = std::numeric_limits< LONGLONG >::max(); - if (g_limit_longlong < uOffset) + if (exceedsMaxLONGLONG(uOffset)) return osl_File_E_OVERFLOW; LONGLONG const nOffset = sal::static_int_cast< LONGLONG >(uOffset); @@ -957,8 +976,7 @@ oslFileError SAL_CALL osl_writeFileAt( if ((pImpl->m_state & FileHandle_Impl::STATE_SEEKABLE) == 0) return osl_File_E_SPIPE; - static sal_uInt64 const g_limit_longlong = std::numeric_limits< LONGLONG >::max(); - if (g_limit_longlong < uOffset) + if (exceedsMaxLONGLONG(uOffset)) return osl_File_E_OVERFLOW; LONGLONG const nOffset = sal::static_int_cast< LONGLONG >(uOffset); @@ -996,8 +1014,7 @@ oslFileError SAL_CALL osl_setFilePos(oslFileHandle Handle, sal_uInt32 uHow, sal_ if ((!pImpl) || !IsValidHandle(pImpl->m_hFile)) return osl_File_E_INVAL; - static sal_Int64 const g_limit_longlong = std::numeric_limits< LONGLONG >::max(); - if (g_limit_longlong < uOffset) + if (exceedsMaxLONGLONG(uOffset) || exceedsMinLONGLONG(uOffset)) return osl_File_E_OVERFLOW; LONGLONG nPos = 0, nOffset = sal::static_int_cast< LONGLONG >(uOffset); @@ -1013,7 +1030,8 @@ oslFileError SAL_CALL osl_setFilePos(oslFileHandle Handle, sal_uInt32 uHow, sal_ nPos = sal::static_int_cast< LONGLONG >(pImpl->getPos()); if ((nOffset < 0) && (nPos < -1*nOffset)) return osl_File_E_INVAL; - if (g_limit_longlong < nPos + nOffset) + assert(nPos >= 0); + if (nOffset > g_limit_longlong - nPos) return osl_File_E_OVERFLOW; break; @@ -1021,7 +1039,8 @@ oslFileError SAL_CALL osl_setFilePos(oslFileHandle Handle, sal_uInt32 uHow, sal_ nPos = sal::static_int_cast< LONGLONG >(pImpl->getSize()); if ((nOffset < 0) && (nPos < -1*nOffset)) return osl_File_E_INVAL; - if (g_limit_longlong < nPos + nOffset) + assert(nPos >= 0); + if (nOffset > g_limit_longlong - nPos) return osl_File_E_OVERFLOW; break; @@ -1053,8 +1072,7 @@ oslFileError SAL_CALL osl_setFileSize(oslFileHandle Handle, sal_uInt64 uSize) if ((pImpl->m_state & FileHandle_Impl::STATE_WRITEABLE) == 0) return osl_File_E_BADF; - static sal_uInt64 const g_limit_longlong = std::numeric_limits< LONGLONG >::max(); - if (g_limit_longlong < uSize) + if (exceedsMaxLONGLONG(uSize)) return osl_File_E_OVERFLOW; FileHandle_Impl::Guard lock(&(pImpl->m_mutex)); |