1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-26 03:22:00 +03:00

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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2021-08-02 15:44:56 +02:00
parent 169d980bc8
commit 8b06c72969

View File

@ -285,10 +285,8 @@ static bool test_key(sd_device *dev,
const unsigned long* bitmask_ev, const unsigned long* bitmask_ev,
const unsigned long* bitmask_key, const unsigned long* bitmask_key,
bool test) { bool test) {
unsigned i;
unsigned long found; bool found = false;
unsigned long mask;
bool ret = false;
/* do we have any KEY_* capability? */ /* do we have any KEY_* capability? */
if (!test_bit(EV_KEY, bitmask_ev)) { if (!test_bit(EV_KEY, bitmask_ev)) {
@ -297,39 +295,32 @@ static bool test_key(sd_device *dev,
} }
/* only consider KEY_* here, not BTN_* */ /* only consider KEY_* here, not BTN_* */
found = 0; for (size_t i = 0; i < BTN_MISC/BITS_PER_LONG && !found; i++) {
for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) { if (bitmask_key[i])
found |= bitmask_key[i]; found = true;
log_device_debug(dev, "test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i*BITS_PER_LONG, found > 0);
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 there are no keys in the lower block, check the higher blocks */
if (!found) { for (size_t block = 0; block < sizeof(high_key_blocks) / sizeof(struct range) && !found; block++)
unsigned block; for (unsigned i = high_key_blocks[block].start; i < high_key_blocks[block].end && !found; i++)
for (block = 0; block < (sizeof(high_key_blocks) / sizeof(struct range)); ++block) { if (test_bit(i, bitmask_key)) {
for (i = high_key_blocks[block].start; i < high_key_blocks[block].end; ++i) { log_device_debug(dev, "test_key: Found key %x in high block", i);
if (test_bit(i, bitmask_key)) { found = true;
log_device_debug(dev, "test_key: Found key %x in high block", i);
found = 1;
break;
}
} }
}
}
if (found > 0) { if (found)
udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1"); 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 /* 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 */ * those, consider it a full keyboard; do not test KEY_RESERVED, though */
mask = 0xFFFFFFFE; if (FLAGS_SET(bitmask_key[0], 0xFFFFFFFE)) {
if (FLAGS_SET(bitmask_key[0], mask)) {
udev_builtin_add_property(dev, test, "ID_INPUT_KEYBOARD", "1"); 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) { static int builtin_input_id(sd_device *dev, int argc, char *argv[], bool test) {