diff --git a/dbus/dbus-string.h b/dbus/dbus-string.h index 95a03099..adf709bd 100644 --- a/dbus/dbus-string.h +++ b/dbus/dbus-string.h @@ -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); diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 0bcd5abd..c7069521 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -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"); }