Avoid unnecessary VLAs
authorMichael Forney <mforney@mforney.org>
Sat, 15 Jun 2019 20:50:33 +0000 (13:50 -0700)
committerMichael Forney <mforney@mforney.org>
Sat, 15 Jun 2019 22:24:10 +0000 (15:24 -0700)
When the array length is fixed, or bounded by a fixed upper bound,
just use that fixed length.

Signed-off-by: Michael Forney <mforney@mforney.org>
src/libinput-util.c
src/quirks.c
test/test-keyboard.c
test/test-pointer.c
test/test-utils.c

index 41f9dae..dcc4a16 100644 (file)
@@ -470,7 +470,8 @@ parse_evcode_property(const char *prop, struct input_event *events, size_t *neve
        bool rc = false;
        size_t ncodes = 0;
        size_t idx;
-       struct input_event evs[*nevents];
+       /* A randomly chosen max so we avoid crazy quirks */
+       struct input_event evs[32];
 
        memset(evs, 0, sizeof evs);
 
@@ -481,8 +482,7 @@ parse_evcode_property(const char *prop, struct input_event *events, size_t *neve
        for (idx = 0; strv[idx]; idx++)
                ncodes++;
 
-       /* A randomly chosen max so we avoid crazy quirks */
-       if (ncodes == 0 || ncodes > 32)
+       if (ncodes == 0 || ncodes > ARRAY_LENGTH(evs))
                goto out;
 
        ncodes = min(*nevents, ncodes);
index e2e8b4f..7e66d1c 100644 (file)
@@ -737,8 +737,8 @@ parse_attr(struct quirks_context *ctx,
                p->value.s = safe_strdup(value);
                rc = true;
        } else if (streq(key, quirk_get_name(QUIRK_ATTR_EVENT_CODE_DISABLE))) {
-               size_t nevents = 32;
-               struct input_event events[nevents];
+               struct input_event events[32];
+               size_t nevents = ARRAY_LENGTH(events);
                p->id = QUIRK_ATTR_EVENT_CODE_DISABLE;
                if (!parse_evcode_property(value, events, &nevents) ||
                    nevents == 0)
index 4b6b531..f5b566d 100644 (file)
@@ -31,8 +31,8 @@
 
 START_TEST(keyboard_seat_key_count)
 {
-       const int num_devices = 4;
-       struct litest_device *devices[num_devices];
+       struct litest_device *devices[4];
+       const int num_devices = ARRAY_LENGTH(devices);
        struct libinput *libinput;
        struct libinput_event *ev;
        struct libinput_event_keyboard *kev;
index 2d71d50..25cef85 100644 (file)
@@ -800,8 +800,8 @@ END_TEST
 
 START_TEST(pointer_seat_button_count)
 {
-       const int num_devices = 4;
-       struct litest_device *devices[num_devices];
+       struct litest_device *devices[4];
+       const int num_devices = ARRAY_LENGTH(devices);
        struct libinput *libinput;
        struct libinput_event *ev;
        struct libinput_event_pointer *tev;
index 356cd43..0cde5aa 100644 (file)
@@ -517,8 +517,8 @@ START_TEST(evcode_prop_parser)
 
        for (int i = 0; tests[i].prop; i++) {
                bool success;
-               size_t nevents = 32;
-               struct input_event events[nevents];
+               struct input_event events[32];
+               size_t nevents = ARRAY_LENGTH(events);
 
                t = &tests[i];
                success = parse_evcode_property(t->prop, events, &nevents);