Commit ce6e68ac authored by Christian Kellner's avatar Christian Kellner

common: enums: stronger precondition for enums

Passing an invalid type (or NULL) to the bolt_enum_xxx functions
is a programming error and such be treated as such. Therefore
the error checks are turned into pre-condition checks.
parent 6585e550
......@@ -38,15 +38,7 @@ bolt_enum_class_validate (GEnumClass *enum_class,
const char *name;
gboolean oob;
if (enum_class == NULL)
{
name = g_type_name_from_class ((GTypeClass *) enum_class);
g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
"could not determine enum class for '%s'",
name);
return FALSE;
}
g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), FALSE);
oob = value < enum_class->minimum || value > enum_class->maximum;
......@@ -67,7 +59,12 @@ bolt_enum_validate (GType enum_type,
gint value,
GError **error)
{
g_autoptr(GEnumClass) klass = g_type_class_ref (enum_type);
g_autoptr(GEnumClass) klass = NULL;
g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), FALSE);
klass = g_type_class_ref (enum_type);
return bolt_enum_class_validate (klass, value, error);
}
......@@ -79,6 +76,8 @@ bolt_enum_to_string (GType enum_type,
g_autoptr(GEnumClass) klass = NULL;
GEnumValue *ev;
g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), FALSE);
klass = g_type_class_ref (enum_type);
if (!bolt_enum_class_validate (klass, value, error))
......@@ -97,12 +96,7 @@ bolt_enum_from_string (GType enum_type,
const char *name;
GEnumValue *ev;
if (!G_TYPE_IS_ENUM (enum_type))
{
g_set_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS,
"supplied type not an enum type");
return -1;
}
g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), FALSE);
klass = g_type_class_ref (enum_type);
g_return_val_if_fail (klass != NULL, -1);
......
......@@ -111,11 +111,6 @@ test_enums (TestRng *tt, gconstpointer user_data)
g_assert_cmpuint (bolt_security_from_string ("user"), ==, BOLT_SECURITY_USER);
g_assert_cmpuint (bolt_security_from_string ("secure"), ==, BOLT_SECURITY_SECURE);
ok = bolt_enum_class_validate (NULL, 0, &err);
g_assert_error (err, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
g_assert_false (ok);
g_clear_pointer (&err, g_error_free);
klass = g_type_class_ref (BOLT_TYPE_SECURITY);
ok = bolt_enum_class_validate (klass, klass->minimum, &err);
......@@ -158,12 +153,6 @@ test_enums (TestRng *tt, gconstpointer user_data)
g_assert_nonnull (err);
g_assert_cmpint (val, ==, -1);
g_clear_error (&err);
/* not a valid enum type */
val = bolt_enum_from_string (BOLT_TYPE_KITT_FLAGS, "ILEDELI", &err);
g_assert_nonnull (err);
g_assert_cmpint (val, ==, -1);
g_clear_error (&err);
}
static void
......
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