gobject: Add g_{param_spec,signal}_is_valid_name() functions

Making this validation code public allows projects to validate a
GParamSpec name before creating it. While hard-coded GParamSpec don't
need this, we can't afford crashing the main program for dynamically
generated GParamSpec from user-created data.

In such case, we will need to validate the param names and return errors
instead of trying to create a GParamSpec with invalid names.

Includes modifications from Philip Withnall and Emmanuele Bassi to
rearrange the new function addition and split it into one function for
GParamSpecs and one for GSignals.
This commit is contained in:
Jehan
2019-12-23 18:36:21 +01:00
committed by Philip Withnall
parent fb1e416a32
commit 13d1697b67
7 changed files with 104 additions and 33 deletions

View File

@@ -1449,7 +1449,33 @@ test_signals_invalid_name (gconstpointer test_data)
g_test_trap_subprocess (NULL, 0, 0);
g_test_trap_assert_failed ();
g_test_trap_assert_stderr ("*CRITICAL*is_valid_signal_name (signal_name)*");
g_test_trap_assert_stderr ("*CRITICAL*g_signal_is_valid_name (signal_name)*");
}
static void
test_signal_is_valid_name (void)
{
const gchar *valid_names[] =
{
"signal",
"i",
"multiple-segments",
"segment0-SEGMENT1",
"using_underscores",
};
const gchar *invalid_names[] =
{
"",
"7zip",
"my_int:hello",
};
gsize i;
for (i = 0; i < G_N_ELEMENTS (valid_names); i++)
g_assert_true (g_signal_is_valid_name (valid_names[i]));
for (i = 0; i < G_N_ELEMENTS (invalid_names); i++)
g_assert_false (g_signal_is_valid_name (invalid_names[i]));
}
/* --- */
@@ -1485,6 +1511,7 @@ main (int argc,
g_test_add_data_func ("/gobject/signals/invalid-name/colon", "my_int:hello", test_signals_invalid_name);
g_test_add_data_func ("/gobject/signals/invalid-name/first-char", "7zip", test_signals_invalid_name);
g_test_add_data_func ("/gobject/signals/invalid-name/empty", "", test_signals_invalid_name);
g_test_add_func ("/gobject/signals/is-valid-name", test_signal_is_valid_name);
return g_test_run ();
}