Commit 09c9d640 authored by Simon McVittie's avatar Simon McVittie

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=20496Reviewed-by: Will Thompson's avatarWill Thompson <will.thompson@collabora.co.uk>
parent 8299a41a
......@@ -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)
......
......@@ -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.
*
......
......@@ -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);
......
......@@ -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;
}
......
......@@ -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 <stdio.h>
......@@ -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);
......
......@@ -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
......
......@@ -78,6 +78,9 @@ DBUS_EXPORT
dbus_bool_t dbus_signature_validate_single (const char *signature,
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
......
......@@ -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;
......
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