From 09c9d6406b75f161429e16b2e45dcae3eb60cef9 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 3 Mar 2011 17:35:14 +0000 Subject: [PATCH 1/1] Make dbus_type_is_valid into public API This is just as useful for bindings as dbus_signature_validate, and I think it's a good design principle to say that anything checked in a _dbus_return_if_fail should be something the caller could check for themselves. Bug: https://bugs.freedesktop.org/show_bug.cgi?id=20496 Reviewed-by: Will Thompson --- dbus/dbus-connection.c | 3 ++- dbus/dbus-marshal-basic.c | 38 ------------------------------- dbus/dbus-marshal-basic.h | 1 - dbus/dbus-marshal-validate.c | 4 ++-- dbus/dbus-message-factory.c | 3 ++- dbus/dbus-signature.c | 53 +++++++++++++++++++++++++++++++++++++++----- dbus/dbus-signature.h | 3 +++ test/break-loader.c | 2 +- 8 files changed, 57 insertions(+), 50 deletions(-) diff --git a/dbus/dbus-connection.c b/dbus/dbus-connection.c index 6779b6a..526a716 100644 --- a/dbus/dbus-connection.c +++ b/dbus/dbus-connection.c @@ -38,6 +38,7 @@ #include "dbus-protocol.h" #include "dbus-dataslot.h" #include "dbus-string.h" +#include "dbus-signature.h" #include "dbus-pending-call.h" #include "dbus-object-tree.h" #include "dbus-threads-internal.h" @@ -3095,7 +3096,7 @@ dbus_connection_can_send_type(DBusConnection *connection, { _dbus_return_val_if_fail (connection != NULL, FALSE); - if (!_dbus_type_is_valid(type)) + if (!dbus_type_is_valid (type)) return FALSE; if (type != DBUS_TYPE_UNIX_FD) diff --git a/dbus/dbus-marshal-basic.c b/dbus/dbus-marshal-basic.c index 3cbc721..486f1c0 100644 --- a/dbus/dbus-marshal-basic.c +++ b/dbus/dbus-marshal-basic.c @@ -1231,44 +1231,6 @@ _dbus_type_get_alignment (int typecode) } } - -/** - * Return #TRUE if the typecode is a valid typecode. - * #DBUS_TYPE_INVALID surprisingly enough is not considered valid, and - * random unknown bytes aren't either. This function is safe with - * untrusted data. - * - * @returns #TRUE if valid - */ -dbus_bool_t -_dbus_type_is_valid (int typecode) -{ - switch (typecode) - { - case DBUS_TYPE_BYTE: - case DBUS_TYPE_BOOLEAN: - case DBUS_TYPE_INT16: - case DBUS_TYPE_UINT16: - case DBUS_TYPE_INT32: - case DBUS_TYPE_UINT32: - case DBUS_TYPE_INT64: - case DBUS_TYPE_UINT64: - case DBUS_TYPE_DOUBLE: - case DBUS_TYPE_STRING: - case DBUS_TYPE_OBJECT_PATH: - case DBUS_TYPE_SIGNATURE: - case DBUS_TYPE_ARRAY: - case DBUS_TYPE_STRUCT: - case DBUS_TYPE_DICT_ENTRY: - case DBUS_TYPE_VARIANT: - case DBUS_TYPE_UNIX_FD: - return TRUE; - - default: - return FALSE; - } -} - /** * Returns a string describing the given type. * diff --git a/dbus/dbus-marshal-basic.h b/dbus/dbus-marshal-basic.h index 0c27fc9..3c448df 100644 --- a/dbus/dbus-marshal-basic.h +++ b/dbus/dbus-marshal-basic.h @@ -254,7 +254,6 @@ dbus_uint32_t _dbus_marshal_read_uint32 (const DBusString *str, int pos, int byte_order, int *new_pos); -dbus_bool_t _dbus_type_is_valid (int typecode); int _dbus_type_get_alignment (int typecode); dbus_bool_t _dbus_type_is_fixed (int typecode); int _dbus_type_get_alignment (int typecode); diff --git a/dbus/dbus-marshal-validate.c b/dbus/dbus-marshal-validate.c index 4304467..d87a27b 100644 --- a/dbus/dbus-marshal-validate.c +++ b/dbus/dbus-marshal-validate.c @@ -250,7 +250,7 @@ _dbus_validate_signature_with_reason (const DBusString *type_str, if (last == DBUS_DICT_ENTRY_BEGIN_CHAR) { - if (!(_dbus_type_is_valid (*p) && dbus_type_is_basic (*p))) + if (!(dbus_type_is_valid (*p) && dbus_type_is_basic (*p))) { result = DBUS_INVALID_DICT_KEY_MUST_BE_BASIC_TYPE; goto out; @@ -393,7 +393,7 @@ validate_body_helper (DBusTypeReader *reader, { int array_elem_type = _dbus_type_reader_get_element_type (reader); - if (!_dbus_type_is_valid (array_elem_type)) + if (!dbus_type_is_valid (array_elem_type)) { return DBUS_INVALID_UNKNOWN_TYPECODE; } diff --git a/dbus/dbus-message-factory.c b/dbus/dbus-message-factory.c index 7ecf827..7fae583 100644 --- a/dbus/dbus-message-factory.c +++ b/dbus/dbus-message-factory.c @@ -27,6 +27,7 @@ #ifdef DBUS_BUILD_TESTS #include "dbus-message-factory.h" #include "dbus-message-private.h" +#include "dbus-signature.h" #include "dbus-test.h" #include @@ -978,7 +979,7 @@ find_next_typecode (DBusMessageDataIter *iter, _dbus_assert (byte_seq < _dbus_string_get_length (data)); - if (_dbus_type_is_valid (_dbus_string_get_byte (data, byte_seq))) + if (dbus_type_is_valid (_dbus_string_get_byte (data, byte_seq))) break; else iter_next (iter); diff --git a/dbus/dbus-signature.c b/dbus/dbus-signature.c index 9c13ff4..c130de5 100644 --- a/dbus/dbus-signature.c +++ b/dbus/dbus-signature.c @@ -284,7 +284,8 @@ dbus_signature_validate_single (const char *signature, * container types. #DBUS_TYPE_INVALID is not a container type. * * It is an error to pass an invalid type-code, other than DBUS_TYPE_INVALID, - * to this function. The valid type-codes are defined by dbus-protocol.h. + * to this function. The valid type-codes are defined by dbus-protocol.h + * and can be checked with dbus_type_is_valid(). * * @param typecode either a valid type-code or DBUS_TYPE_INVALID * @returns #TRUE if type is a container @@ -293,7 +294,7 @@ dbus_bool_t dbus_type_is_container (int typecode) { /* only reasonable (non-line-noise) typecodes are allowed */ - _dbus_return_val_if_fail (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID, + _dbus_return_val_if_fail (dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID, FALSE); return TYPE_IS_CONTAINER (typecode); } @@ -307,7 +308,8 @@ dbus_type_is_container (int typecode) * type. * * It is an error to pass an invalid type-code, other than DBUS_TYPE_INVALID, - * to this function. The valid type-codes are defined by dbus-protocol.h. + * to this function. The valid type-codes are defined by dbus-protocol.h + * and can be checked with dbus_type_is_valid(). * * @param typecode either a valid type-code or DBUS_TYPE_INVALID * @returns #TRUE if type is basic @@ -316,7 +318,7 @@ dbus_bool_t dbus_type_is_basic (int typecode) { /* only reasonable (non-line-noise) typecodes are allowed */ - _dbus_return_val_if_fail (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID, + _dbus_return_val_if_fail (dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID, FALSE); /* everything that isn't invalid or a container */ @@ -337,7 +339,8 @@ dbus_type_is_basic (int typecode) * function. * * It is an error to pass an invalid type-code, other than DBUS_TYPE_INVALID, - * to this function. The valid type-codes are defined by dbus-protocol.h. + * to this function. The valid type-codes are defined by dbus-protocol.h + * and can be checked with dbus_type_is_valid(). * * @param typecode either a valid type-code or DBUS_TYPE_INVALID * @returns #FALSE if the type can occupy different lengths @@ -346,7 +349,7 @@ dbus_bool_t dbus_type_is_fixed (int typecode) { /* only reasonable (non-line-noise) typecodes are allowed */ - _dbus_return_val_if_fail (_dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID, + _dbus_return_val_if_fail (dbus_type_is_valid (typecode) || typecode == DBUS_TYPE_INVALID, FALSE); switch (typecode) @@ -367,6 +370,44 @@ dbus_type_is_fixed (int typecode) } } +/** + * Return #TRUE if the argument is a valid typecode. + * #DBUS_TYPE_INVALID surprisingly enough is not considered valid, and + * random unknown bytes aren't either. This function is safe with + * untrusted data. + * + * @param typecode a potential type-code + * @returns #TRUE if valid + */ +dbus_bool_t +dbus_type_is_valid (int typecode) +{ + switch (typecode) + { + case DBUS_TYPE_BYTE: + case DBUS_TYPE_BOOLEAN: + case DBUS_TYPE_INT16: + case DBUS_TYPE_UINT16: + case DBUS_TYPE_INT32: + case DBUS_TYPE_UINT32: + case DBUS_TYPE_INT64: + case DBUS_TYPE_UINT64: + case DBUS_TYPE_DOUBLE: + case DBUS_TYPE_STRING: + case DBUS_TYPE_OBJECT_PATH: + case DBUS_TYPE_SIGNATURE: + case DBUS_TYPE_ARRAY: + case DBUS_TYPE_STRUCT: + case DBUS_TYPE_DICT_ENTRY: + case DBUS_TYPE_VARIANT: + case DBUS_TYPE_UNIX_FD: + return TRUE; + + default: + return FALSE; + } +} + /** @} */ /* end of DBusSignature group */ #ifdef DBUS_BUILD_TESTS diff --git a/dbus/dbus-signature.h b/dbus/dbus-signature.h index ebf00c1..443941c 100644 --- a/dbus/dbus-signature.h +++ b/dbus/dbus-signature.h @@ -79,6 +79,9 @@ dbus_bool_t dbus_signature_validate_single (const char *signatur DBusError *error); DBUS_EXPORT +dbus_bool_t dbus_type_is_valid (int typecode); + +DBUS_EXPORT dbus_bool_t dbus_type_is_basic (int typecode); DBUS_EXPORT dbus_bool_t dbus_type_is_container (int typecode); diff --git a/test/break-loader.c b/test/break-loader.c index 7bfa722..542f36f 100644 --- a/test/break-loader.c +++ b/test/break-loader.c @@ -446,7 +446,7 @@ randomly_change_one_type (const DBusString *orig_data, { int b; b = _dbus_string_get_byte (mutated, i); - if (_dbus_type_is_valid (b)) + if (dbus_type_is_valid (b)) { _dbus_string_set_byte (mutated, i, random_type ()); return; -- 2.7.4