From b4dee67bf573234a17df06ea11529fa040d58be0 Mon Sep 17 00:00:00 2001 From: Dominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com> Date: Sun, 8 Sep 2019 02:04:50 +0200 Subject: [PATCH] Implemented Utility::convert function to convert size_t -> uint safely and on the fly. Often necessary for Qt and WIN32 functions. Using this will not generate compiler warnings of possible truncation. First call implemented in ownsql.cpp Signed-off-by: Dominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com> --- src/common/ownsql.cpp | 4 ++-- src/common/utility.cpp | 10 ++++++++++ src/common/utility.h | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/common/ownsql.cpp b/src/common/ownsql.cpp index 1b3c8c6437..a0669579a4 100644 --- a/src/common/ownsql.cpp +++ b/src/common/ownsql.cpp @@ -281,8 +281,8 @@ int SqlQuery::prepare(const QByteArray &sql, bool allow_failure) */ static bool startsWithInsensitive(const QByteArray &a, const char *b) { - int len = strlen(b); - return a.size() >= len && qstrnicmp(a.constData(), b, len) == 0; + size_t len = strlen(b); + return a.size() >= len && qstrnicmp(a.constData(), b, Utility::convert(len)) == 0; } bool SqlQuery::isSelect() diff --git a/src/common/utility.cpp b/src/common/utility.cpp index 2c2a3e5ef7..6c394f1450 100644 --- a/src/common/utility.cpp +++ b/src/common/utility.cpp @@ -396,6 +396,16 @@ void Utility::crash() *a = 1; } +// Use this function to retrieve uint (often required by Qt and WIN32) from size_t +// without compiler warnings about possible truncation +uint Utility::convert(size_t &convertVar) +{ + if( convertVar > UINT_MAX ) { + throw std::bad_cast(); + } + return static_cast(convertVar); +} + // read the output of the owncloud --version command from the owncloud // version that is on disk. This works for most versions of the client, // because clients that do not yet know the --version flag return the diff --git a/src/common/utility.h b/src/common/utility.h index 5e1ff930eb..3842253918 100644 --- a/src/common/utility.h +++ b/src/common/utility.h @@ -55,6 +55,7 @@ namespace Utility { OCSYNC_EXPORT QByteArray userAgentString(); OCSYNC_EXPORT bool hasLaunchOnStartup(const QString &appName); OCSYNC_EXPORT void setLaunchOnStartup(const QString &appName, const QString &guiName, bool launch); + OCSYNC_EXPORT uint convert(size_t &convertVar); /** * Return the amount of free space available.