]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev-builtin-input_ic: simplify loop in test_key()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 2 Aug 2021 13:44:56 +0000 (15:44 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 7 Aug 2021 07:00:55 +0000 (09:00 +0200)
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

index dda53b6da0f25dee7d73e8626bd66e0b30d9dc51..6921cfba5857e94da1756ba4d6507d64412c224c 100644 (file)
@@ -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) {