app: Check validity of hotkey

The hotkey is valid if it has a valid value. The value is valid if it is
not empty and is successfully parsed by gtk_accelerator_parse().

These hotkeys formats are considered invalid:
 "key" - missing value
 "key=" - missing value
 "key=abcd" - value cannot be parsed by gtk_accelerator_parse()

Resolves: rhbz#1339572

Acked-by: Fabiano Fidêncio <fidencio@redhat.com>
This commit is contained in:
Pavel Grunt 2016-05-31 11:01:24 +02:00
parent 9a3041eb00
commit ffa460b8c6
2 changed files with 18 additions and 5 deletions

View File

@ -2065,18 +2065,23 @@ virt_viewer_app_set_hotkeys(VirtViewerApp *self, const gchar *hotkeys_str)
for (hotkey = hotkeys; *hotkey != NULL; hotkey++) {
gchar *key = strstr(*hotkey, "=");
if (key == NULL) {
g_warn_if_reached();
const gchar *value = (key == NULL) ? NULL : (*key = '\0', key + 1);
if (value == NULL || *value == '\0') {
g_warning("missing value for key '%s'", *hotkey);
continue;
}
*key = '\0';
gchar *accel = spice_hotkey_to_gtk_accelerator(key + 1);
gchar *accel = spice_hotkey_to_gtk_accelerator(value);
guint accel_key;
GdkModifierType accel_mods;
gtk_accelerator_parse(accel, &accel_key, &accel_mods);
g_free(accel);
if (accel_key == 0 && accel_mods == 0) {
g_warning("Invalid value '%s' for key '%s'", value, *hotkey);
continue;
}
if (g_str_equal(*hotkey, "toggle-fullscreen")) {
gtk_accel_map_change_entry("<virt-viewer>/view/toggle-fullscreen", accel_key, accel_mods, TRUE);
} else if (g_str_equal(*hotkey, "release-cursor")) {

View File

@ -91,11 +91,19 @@ test_hotkeys_bad(void)
{
"no_value",
G_LOG_LEVEL_WARNING,
"*code should not be reached"
"missing value for key 'no_value'"
},{
"smartcard-insert=",
G_LOG_LEVEL_WARNING,
"missing value for key 'smartcard-insert'"
},{
"toggle-fullscreen=A,unknown_command=B",
G_LOG_LEVEL_WARNING,
"Unknown hotkey command unknown_command"
},{
"secure-attention=value",
G_LOG_LEVEL_WARNING,
"Invalid value 'value' for key 'secure-attention'"
},
};