mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-02-05 17:00:29 +01:00
libnm-core: strip nm_utils_enum_from_str() input string
This commit is contained in:
parent
d1b6835ada
commit
e65854f609
2 changed files with 6 additions and 2 deletions
|
|
@ -4017,14 +4017,16 @@ gboolean nm_utils_enum_from_str (GType type, const char *str,
|
|||
GTypeClass *class;
|
||||
gboolean ret = FALSE;
|
||||
int value = 0;
|
||||
gs_free char *stripped = NULL;
|
||||
|
||||
g_return_val_if_fail (str, FALSE);
|
||||
stripped = g_strstrip (strdup (str));
|
||||
class = g_type_class_ref (type);
|
||||
|
||||
if (G_IS_ENUM_CLASS (class)) {
|
||||
GEnumValue *enum_value;
|
||||
|
||||
enum_value = g_enum_get_value_by_nick (G_ENUM_CLASS (class), str);
|
||||
enum_value = g_enum_get_value_by_nick (G_ENUM_CLASS (class), stripped);
|
||||
if (enum_value) {
|
||||
value = enum_value->value;
|
||||
ret = TRUE;
|
||||
|
|
@ -4034,7 +4036,7 @@ gboolean nm_utils_enum_from_str (GType type, const char *str,
|
|||
gs_strfreev char **strv = NULL;
|
||||
int i;
|
||||
|
||||
strv = g_strsplit (str, ",", 0);
|
||||
strv = g_strsplit (stripped, ",", 0);
|
||||
for (i = 0; strv[i]; i++) {
|
||||
if (!strv[i][0])
|
||||
continue;
|
||||
|
|
|
|||
|
|
@ -4688,12 +4688,14 @@ static void test_nm_utils_enum (void)
|
|||
NM_TEST_GENERAL_COLOR_FLAGS_GREEN, "red,green");
|
||||
|
||||
test_nm_utils_enum_from_str_do (bool_enum, "", FALSE, 0, NULL);
|
||||
test_nm_utils_enum_from_str_do (bool_enum, " ", FALSE, 0, NULL);
|
||||
test_nm_utils_enum_from_str_do (bool_enum, "invalid", FALSE, 0, NULL);
|
||||
test_nm_utils_enum_from_str_do (bool_enum, "yes", TRUE, NM_TEST_GENERAL_BOOL_ENUM_YES, NULL);
|
||||
test_nm_utils_enum_from_str_do (bool_enum, "no", TRUE, NM_TEST_GENERAL_BOOL_ENUM_NO, NULL);
|
||||
test_nm_utils_enum_from_str_do (bool_enum, "yes,no", FALSE, 0, NULL);
|
||||
|
||||
test_nm_utils_enum_from_str_do (meta_flags, "", TRUE, 0, NULL);
|
||||
test_nm_utils_enum_from_str_do (meta_flags, " ", TRUE, 0, NULL);
|
||||
test_nm_utils_enum_from_str_do (meta_flags, "foo", TRUE, NM_TEST_GENERAL_META_FLAGS_FOO, NULL);
|
||||
test_nm_utils_enum_from_str_do (meta_flags, "foo,baz", TRUE, NM_TEST_GENERAL_META_FLAGS_FOO |
|
||||
NM_TEST_GENERAL_META_FLAGS_BAZ, NULL);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue