]> git.mdlowis.com Git - proto/labwc.git/commitdiff
key-state: Prevent array overflow
authorJohn Lindgren <john@jlindgren.net>
Wed, 2 Nov 2022 20:26:33 +0000 (16:26 -0400)
committerJohan Malm <johanmalm@users.noreply.github.com>
Wed, 2 Nov 2022 21:52:33 +0000 (21:52 +0000)
- Prevent adding the same keycode more than once
- Prevent adding more keycodes than MAX_PRESSED_KEYS

src/key-state.c

index 775b2e9b82b6609922a14a3c81d25b2c1c8f87eb..2175e46dd5cc2f5f69aeba4e3dbcd6d856597300 100644 (file)
@@ -12,6 +12,17 @@ struct key_array {
 
 static struct key_array pressed, bound, pressed_sent;
 
+static bool
+key_present(struct key_array *array, uint32_t keycode)
+{
+       for (int i = 0; i < array->nr_keys; ++i) {
+               if (array->keys[i] == keycode) {
+                       return true;
+               }
+       }
+       return false;
+}
+
 static void
 remove_key(struct key_array *array, uint32_t keycode)
 {
@@ -32,7 +43,9 @@ remove_key(struct key_array *array, uint32_t keycode)
 static void
 add_key(struct key_array *array, uint32_t keycode)
 {
-       array->keys[array->nr_keys++] = keycode;
+       if (!key_present(array, keycode) && array->nr_keys < MAX_PRESSED_KEYS) {
+               array->keys[array->nr_keys++] = keycode;
+       }
 }
 
 uint32_t *
@@ -74,12 +87,7 @@ key_state_store_pressed_keys_as_bound(void)
 bool
 key_state_corresponding_press_event_was_bound(uint32_t keycode)
 {
-       for (int i = 0; i < bound.nr_keys; ++i) {
-               if (bound.keys[i] == keycode) {
-                       return true;
-               }
-       }
-       return false;
+       return key_present(&bound, keycode);
 }
 
 void