Implemented Utility::convert function to convert size_t -> uint safely and on the...
authorDominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com>
Sun, 8 Sep 2019 00:04:50 +0000 (02:04 +0200)
committerDominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com>
Sun, 8 Sep 2019 00:04:50 +0000 (02:04 +0200)
Signed-off-by: Dominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com>
src/common/ownsql.cpp
src/common/utility.cpp
src/common/utility.h

index 1b3c8c64376c31b0d7f3aa6ec431e031523c5dfa..a0669579a404183492ff6b9b3f6e06cb62ec3670 100644 (file)
@@ -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()
index 2c2a3e5ef75a57fb96ef490aa11c4eb2b2456b51..6c394f14502cace4761a6b0e0491b307a6cee1d0 100644 (file)
@@ -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<uint>(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
index 5e1ff930eb3f63909cf6896cc6949f849ae628ee..3842253918ccba5a72d115a6d88f2ff4611b8023 100644 (file)
@@ -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.