sal/osl/unx/readwrite_helper.cxx | 34 ++++++++++++++++++++++------------ sal/osl/unx/readwrite_helper.hxx | 8 +++++--- 2 files changed, 27 insertions(+), 15 deletions(-)
New commits: commit a0000aa4c83768aee2b78182c1c40b6bdf810773 Author: Stephan Bergmann <sberg...@redhat.com> AuthorDate: Tue Jan 14 19:55:56 2020 +0100 Commit: Stephan Bergmann <sberg...@redhat.com> CommitDate: Wed Jan 15 08:15:35 2020 +0100 Clean up safeRead/Write ...using more appropriate parameter types, replacing cheesy OSL_ASSERT overflow checks with cap_ssize_t, and replacing one remaining good OSL_ASSERT in safeWrite with assert. Change-Id: I6105ba5135216333e68003458be7ca28f1715a51 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/86807 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sberg...@redhat.com> diff --git a/sal/osl/unx/readwrite_helper.cxx b/sal/osl/unx/readwrite_helper.cxx index c920f339aa61..49c44c2c097b 100644 --- a/sal/osl/unx/readwrite_helper.cxx +++ b/sal/osl/unx/readwrite_helper.cxx @@ -7,20 +7,32 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include <sal/config.h> + +#include <algorithm> +#include <cassert> +#include <cstddef> +#include <limits> + #include "readwrite_helper.hxx" -#include <osl/diagnose.h> #include "system.hxx" -bool safeWrite(int fd, void* data, sal_uInt32 dataSize) +namespace { + +std::size_t cap_ssize_t(std::size_t value) { + return std::min(value, std::size_t(std::numeric_limits<ssize_t>::max())); +} + +} + +bool safeWrite(int fd, void* data, std::size_t dataSize) { - sal_Int32 nToWrite = dataSize; + auto nToWrite = dataSize; unsigned char* dataToWrite = static_cast<unsigned char *>(data); - // Check for overflow as we convert a signed to an unsigned. - OSL_ASSERT(dataSize == static_cast<sal_uInt32>(nToWrite)); while ( nToWrite ) { - sal_Int32 nWritten = write(fd, dataToWrite, nToWrite); + auto nWritten = write(fd, dataToWrite, cap_ssize_t(nToWrite)); if ( nWritten < 0 ) { if ( errno == EINTR ) continue; @@ -29,7 +41,7 @@ bool safeWrite(int fd, void* data, sal_uInt32 dataSize) } - OSL_ASSERT(nWritten > 0); + assert(nWritten > 0); nToWrite -= nWritten; dataToWrite += nWritten; } @@ -37,15 +49,13 @@ bool safeWrite(int fd, void* data, sal_uInt32 dataSize) return true; } -bool safeRead( int fd, void* buffer, sal_uInt32 count ) +bool safeRead( int fd, void* buffer, std::size_t count ) { - sal_Int32 nToRead = count; + auto nToRead = count; unsigned char* bufferForReading = static_cast<unsigned char *>(buffer); - // Check for overflow as we convert a signed to an unsigned. - OSL_ASSERT(count == static_cast<sal_uInt32>(nToRead)); while ( nToRead ) { - sal_Int32 nRead = read(fd, bufferForReading, nToRead); + auto nRead = read(fd, bufferForReading, cap_ssize_t(nToRead)); if ( nRead < 0 ) { // We were interrupted before reading, retry. if (errno == EINTR) diff --git a/sal/osl/unx/readwrite_helper.hxx b/sal/osl/unx/readwrite_helper.hxx index 9ffebe5c112d..d73ce56457cb 100644 --- a/sal/osl/unx/readwrite_helper.hxx +++ b/sal/osl/unx/readwrite_helper.hxx @@ -10,14 +10,16 @@ #ifndef INCLUDED_SAL_OSL_UNX_READWRITE_HELPER_HXX #define INCLUDED_SAL_OSL_UNX_READWRITE_HELPER_HXX -#include <sal/types.h> +#include <sal/config.h> -bool safeWrite( int fd, void* data, sal_uInt32 dataSize ); +#include <cstddef> + +bool safeWrite( int fd, void* data, std::size_t dataSize ); // This function *will* read |count| bytes from |fd|, busy looping // if needed. Don't use it when you don't know if you can request enough // data. It will return sal_False for any partial transfer or error. -bool safeRead( int fd, void* buffer, sal_uInt32 count ); +bool safeRead( int fd, void* buffer, std::size_t count ); #endif _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits