From 9c1a02e26b9540b00ba2b95b6cc41a100761137a Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Wed, 4 Mar 2015 11:05:32 +0000 Subject: [PATCH 1/5] Use new _dbus_string_get_ulength() 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. --- dbus/dbus-string.h | 16 ++++++++++++++++ dbus/dbus-sysdeps-unix.c | 8 ++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/dbus/dbus-string.h b/dbus/dbus-string.h index 95a0309..fed5fe2 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_ulength (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 0bcd5ab..430b7b5 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_ulength (&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_ulength (&buf)) { - _dbus_verbose ("%lu > %d", (unsigned long) len, - _dbus_string_get_length (&buf)); + _dbus_verbose ("%lu > %u", (unsigned long) len, + _dbus_string_get_ulength (&buf)); _dbus_assert_not_reached ("getsockopt(SO_PEERSEC) overflowed"); } -- 2.1.4