]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
Use new _dbus_string_get_length_uint() to avoid another -Wsign-compare
authorSimon McVittie <simon.mcvittie@collabora.co.uk>
Wed, 4 Mar 2015 11:58:45 +0000 (11:58 +0000)
committerSimon McVittie <simon.mcvittie@collabora.co.uk>
Wed, 4 Mar 2015 18:40:10 +0000 (18:40 +0000)
DBusString's length is signed for historical reasons: the right type
would have been size_t or some other unsigned type. We have a *lot*
of callers of _dbus_string_get_length(), so it is not really desirable
to do a flag-day switch; but we know that the length is always in the
range [0, INT_MAX] that is common to int and unsigned int, so we can
safely add an unsigned accessor.

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=17289
Reviewed-by: Ralf Habacker <ralf.habacker@freenet.de>
dbus/dbus-string.h
dbus/dbus-sysdeps-unix.c

index 95a030996bdb54049d09bd7beeaf1e4ecfc15728..adf709bddf03a0e0524f93f8f847b220b09c303a 100644 (file)
@@ -151,6 +151,22 @@ DBUS_PRIVATE_EXPORT
 int           _dbus_string_get_length            (const DBusString  *str);
 #endif /* !_dbus_string_get_length */
 
+/**
+ * Get the string's length as an unsigned integer, for comparison with
+ * size_t and similar unsigned types that does not trigger compiler
+ * warnings about potential value changes during conversion.
+ *
+ * DBusString lengths are signed for historical reasons, but we know that
+ * the length is always >= 0 (and DBUS_GENERIC_STRING_PREAMBLE asserts
+ * that this is the case) so we know that this cast does not change the
+ * value.
+ */
+static inline unsigned int
+_dbus_string_get_length_uint (const DBusString *str)
+{
+  return (unsigned int) _dbus_string_get_length (str);
+}
+
 DBUS_PRIVATE_EXPORT
 dbus_bool_t   _dbus_string_lengthen              (DBusString        *str,
                                                   int                additional_length);
index 0bcd5abd6a7e30e628d1964f4508709d2e6fed99..c7069521a5aa77b370875727ee3395440ba8b87a 100644 (file)
@@ -1689,7 +1689,7 @@ add_linux_security_label_to_credentials (int              client_fd,
       _dbus_verbose ("getsockopt failed with %s, len now %lu\n",
                      _dbus_strerror (e), (unsigned long) len);
 
-      if (e != ERANGE || len <= _dbus_string_get_length (&buf))
+      if (e != ERANGE || len <= _dbus_string_get_length_uint (&buf))
         {
           _dbus_verbose ("Failed to getsockopt(SO_PEERSEC): %s\n",
                          _dbus_strerror (e));
@@ -1714,10 +1714,10 @@ add_linux_security_label_to_credentials (int              client_fd,
       goto out;
     }
 
-  if (len > _dbus_string_get_length (&buf))
+  if (len > _dbus_string_get_length_uint (&buf))
     {
-      _dbus_verbose ("%lu > %d", (unsigned long) len,
-                     _dbus_string_get_length (&buf));
+      _dbus_verbose ("%lu > %u", (unsigned long) len,
+                     _dbus_string_get_length_uint (&buf));
       _dbus_assert_not_reached ("getsockopt(SO_PEERSEC) overflowed");
     }