From 8b06c72969ee0af65a06cbeb2222e8108ca41d30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 2 Aug 2021 15:44:56 +0200 Subject: [PATCH] udev-builtin-input_ic: simplify loop in test_key() We would update 'found' using bit operations, but studiously ignore the actual value and treat it as boolean. So just use a boolean variable instead. Because there is a double loop, we would break the inner loop, but repeat the outer loop, even though the boolean was already set. Add '&& !found' in the loop conditions to break iteration immediately. --- src/udev/udev-builtin-input_id.c | 43 +++++++++++++------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/src/udev/udev-builtin-input_id.c b/src/udev/udev-builtin-input_id.c index dda53b6da0f..6921cfba585 100644 --- a/src/udev/udev-builtin-input_id.c +++ b/src/udev/udev-builtin-input_id.c @@ -285,10 +285,8 @@ static bool test_key(sd_device *dev, const unsigned long* bitmask_ev, const unsigned long* bitmask_key, bool test) { - unsigned i; - unsigned long found; - unsigned long mask; - bool ret = false; + + bool found = false; /* do we have any KEY_* capability? */ if (!test_bit(EV_KEY, bitmask_ev)) { @@ -297,39 +295,32 @@ static bool test_key(sd_device *dev, } /* only consider KEY_* here, not BTN_* */ - found = 0; - for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) { - found |= bitmask_key[i]; - log_device_debug(dev, "test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i*BITS_PER_LONG, found > 0); + for (size_t i = 0; i < BTN_MISC/BITS_PER_LONG && !found; i++) { + if (bitmask_key[i]) + found = true; + + log_device_debug(dev, "test_key: checking bit block %zu for any keys; found=%s", + i * BITS_PER_LONG, yes_no(found)); } /* If there are no keys in the lower block, check the higher blocks */ - if (!found) { - unsigned block; - for (block = 0; block < (sizeof(high_key_blocks) / sizeof(struct range)); ++block) { - for (i = high_key_blocks[block].start; i < high_key_blocks[block].end; ++i) { - if (test_bit(i, bitmask_key)) { - log_device_debug(dev, "test_key: Found key %x in high block", i); - found = 1; - break; - } + for (size_t block = 0; block < sizeof(high_key_blocks) / sizeof(struct range) && !found; block++) + for (unsigned i = high_key_blocks[block].start; i < high_key_blocks[block].end && !found; i++) + if (test_bit(i, bitmask_key)) { + log_device_debug(dev, "test_key: Found key %x in high block", i); + found = true; } - } - } - if (found > 0) { + if (found) udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1"); - ret = true; - } /* the first 32 bits are ESC, numbers, and Q to D; if we have all of * those, consider it a full keyboard; do not test KEY_RESERVED, though */ - mask = 0xFFFFFFFE; - if (FLAGS_SET(bitmask_key[0], mask)) { + if (FLAGS_SET(bitmask_key[0], 0xFFFFFFFE)) { udev_builtin_add_property(dev, test, "ID_INPUT_KEYBOARD", "1"); - ret = true; + return true; } - return ret; + return found; } static int builtin_input_id(sd_device *dev, int argc, char *argv[], bool test) {