Commit 51a718c6 authored by Simon McVittie's avatar Simon McVittie

Use new _dbus_string_get_length_uint() to avoid another -Wsign-compare

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=17289Reviewed-by: Ralf Habacker's avatarRalf Habacker <ralf.habacker@freenet.de>
parent 43d2455e
......@@ -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);
......
......@@ -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");
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment