From 29318ad832540b538cd2ddd23edd8ffc076e2a6f Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 11 Sep 2014 12:55:52 +0100 Subject: [PATCH 2/2] Replace some runtime assertions with compile-time assertions This requires a little bit of code re-ordering, because _DBUS_STATIC_ASSERT can appear anywhere that a variable declaration would be valid, i.e. not after executable code. --- dbus/dbus-errors.c | 4 ++-- dbus/dbus-hash.c | 6 +++--- dbus/dbus-marshal-basic.c | 6 +++--- dbus/dbus-message.c | 2 +- dbus/dbus-signature.c | 2 +- dbus/dbus-string.c | 6 +++--- dbus/dbus-sysdeps-unix.c | 22 +++++++++++----------- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/dbus/dbus-errors.c b/dbus/dbus-errors.c index a0571a5..5d8f194 100644 --- a/dbus/dbus-errors.c +++ b/dbus/dbus-errors.c @@ -189,9 +189,9 @@ dbus_error_init (DBusError *error) { DBusRealError *real; - _dbus_return_if_fail (error != NULL); + _DBUS_STATIC_ASSERT (sizeof (DBusError) == sizeof (DBusRealError)); - _dbus_assert (sizeof (DBusError) == sizeof (DBusRealError)); + _dbus_return_if_fail (error != NULL); real = (DBusRealError *)error; diff --git a/dbus/dbus-hash.c b/dbus/dbus-hash.c index c80835a..8f7d04b 100644 --- a/dbus/dbus-hash.c +++ b/dbus/dbus-hash.c @@ -519,7 +519,7 @@ _dbus_hash_iter_init (DBusHashTable *table, { DBusRealHashIter *real; - _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter)); + _DBUS_STATIC_ASSERT (sizeof (DBusHashIter) == sizeof (DBusRealHashIter)); real = (DBusRealHashIter*) iter; @@ -544,7 +544,7 @@ _dbus_hash_iter_next (DBusHashIter *iter) { DBusRealHashIter *real; - _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter)); + _DBUS_STATIC_ASSERT (sizeof (DBusHashIter) == sizeof (DBusRealHashIter)); real = (DBusRealHashIter*) iter; @@ -746,7 +746,7 @@ _dbus_hash_iter_lookup (DBusHashTable *table, DBusHashEntry *entry; DBusHashEntry **bucket; - _dbus_assert (sizeof (DBusHashIter) == sizeof (DBusRealHashIter)); + _DBUS_STATIC_ASSERT (sizeof (DBusHashIter) == sizeof (DBusRealHashIter)); real = (DBusRealHashIter*) iter; diff --git a/dbus/dbus-marshal-basic.c b/dbus/dbus-marshal-basic.c index eafc2a9..9bb5b76 100644 --- a/dbus/dbus-marshal-basic.c +++ b/dbus/dbus-marshal-basic.c @@ -599,7 +599,7 @@ marshal_2_octets (DBusString *str, dbus_bool_t retval; int orig_len; - _dbus_assert (sizeof (value) == 2); + _DBUS_STATIC_ASSERT (sizeof (value) == 2); if (byte_order != DBUS_COMPILER_BYTE_ORDER) value = DBUS_UINT16_SWAP_LE_BE (value); @@ -628,7 +628,7 @@ marshal_4_octets (DBusString *str, dbus_bool_t retval; int orig_len; - _dbus_assert (sizeof (value) == 4); + _DBUS_STATIC_ASSERT (sizeof (value) == 4); if (byte_order != DBUS_COMPILER_BYTE_ORDER) value = DBUS_UINT32_SWAP_LE_BE (value); @@ -657,7 +657,7 @@ marshal_8_octets (DBusString *str, dbus_bool_t retval; int orig_len; - _dbus_assert (sizeof (value) == 8); + _DBUS_STATIC_ASSERT (sizeof (value) == 8); swap_8_octets (&value, byte_order); diff --git a/dbus/dbus-message.c b/dbus/dbus-message.c index 78df755..203a016 100644 --- a/dbus/dbus-message.c +++ b/dbus/dbus-message.c @@ -2024,7 +2024,7 @@ _dbus_message_iter_init_common (DBusMessage *message, DBusMessageRealIter *real, int iter_type) { - _dbus_assert (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter)); + _DBUS_STATIC_ASSERT (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter)); /* Since the iterator will read or write who-knows-what from the * message, we need to get in the right byte order diff --git a/dbus/dbus-signature.c b/dbus/dbus-signature.c index 8a4701c..6f1521b 100644 --- a/dbus/dbus-signature.c +++ b/dbus/dbus-signature.c @@ -428,7 +428,7 @@ _dbus_signature_test (void) const char *sig; dbus_bool_t boolres; - _dbus_assert (sizeof (DBusSignatureIter) >= sizeof (DBusSignatureRealIter)); + _DBUS_STATIC_ASSERT (sizeof (DBusSignatureIter) >= sizeof (DBusSignatureRealIter)); sig = ""; _dbus_assert (dbus_signature_validate (sig, NULL)); diff --git a/dbus/dbus-string.c b/dbus/dbus-string.c index 2001074..15cff9c 100644 --- a/dbus/dbus-string.c +++ b/dbus/dbus-string.c @@ -133,11 +133,11 @@ _dbus_string_init_preallocated (DBusString *str, int allocate_size) { DBusRealString *real; - + + _DBUS_STATIC_ASSERT (sizeof (DBusString) == sizeof (DBusRealString)); + _dbus_assert (str != NULL); - _dbus_assert (sizeof (DBusString) == sizeof (DBusRealString)); - real = (DBusRealString*) str; /* It's very important not to touch anything diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 106c689..a5090d4 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -1660,18 +1660,18 @@ _dbus_read_credentials_socket (int client_fd, } cmsg; #endif - uid_read = DBUS_UID_UNSET; - pid_read = DBUS_PID_UNSET; - - _DBUS_ASSERT_ERROR_IS_CLEAR (error); - /* The POSIX spec certainly doesn't promise this, but * we need these assertions to fail as soon as we're wrong about * it so we can do the porting fixups */ - _dbus_assert (sizeof (pid_t) <= sizeof (dbus_pid_t)); - _dbus_assert (sizeof (uid_t) <= sizeof (dbus_uid_t)); - _dbus_assert (sizeof (gid_t) <= sizeof (dbus_gid_t)); + _DBUS_STATIC_ASSERT (sizeof (pid_t) <= sizeof (dbus_pid_t)); + _DBUS_STATIC_ASSERT (sizeof (uid_t) <= sizeof (dbus_uid_t)); + _DBUS_STATIC_ASSERT (sizeof (gid_t) <= sizeof (dbus_gid_t)); + + uid_read = DBUS_UID_UNSET; + pid_read = DBUS_PID_UNSET; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); _dbus_credentials_clear (credentials); @@ -2352,9 +2352,9 @@ _dbus_credentials_add_from_current_process (DBusCredentials *credentials) * we need these assertions to fail as soon as we're wrong about * it so we can do the porting fixups */ - _dbus_assert (sizeof (pid_t) <= sizeof (dbus_pid_t)); - _dbus_assert (sizeof (uid_t) <= sizeof (dbus_uid_t)); - _dbus_assert (sizeof (gid_t) <= sizeof (dbus_gid_t)); + _DBUS_STATIC_ASSERT (sizeof (pid_t) <= sizeof (dbus_pid_t)); + _DBUS_STATIC_ASSERT (sizeof (uid_t) <= sizeof (dbus_uid_t)); + _DBUS_STATIC_ASSERT (sizeof (gid_t) <= sizeof (dbus_gid_t)); if (!_dbus_credentials_add_pid(credentials, _dbus_getpid())) return FALSE; -- 2.1.0