From 2c0c4a6516083563ecde3fe53003390b4e351f71 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 9 Jan 2025 09:04:54 +1000 Subject: [PATCH] Replace strneq() with hardcoded lengths with strstartswith() Slightly less efficient but easier to read and it's not possible to accidentally provide the wrong length. Plus it handles null pointers correctly so get to skip the checks (which weren't needed for strneq() either, but still). Part-of: --- src/quirks.c | 18 +++++++++--------- src/udev-seat.c | 4 ++-- src/util-prop-parsers.c | 2 +- test/litest-device-keyboard-all-codes.c | 2 +- test/litest.c | 6 +++--- test/test-device.c | 4 ++-- test/test-keyboard.c | 2 +- test/test-path.c | 2 +- test/test-udev.c | 2 +- tools/libinput-quirks.c | 2 +- tools/libinput-record.c | 18 +++++++++--------- tools/shared.c | 2 +- 12 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/quirks.c b/src/quirks.c index 2bb37b4f..6d45f4ac 100644 --- a/src/quirks.c +++ b/src/quirks.c @@ -531,7 +531,7 @@ section_destroy(struct section *s) static inline bool parse_hex(const char *value, unsigned int *parsed) { - return strneq(value, "0x", 2) && + return strstartswith(value, "0x") && safe_atou_base(value, parsed, 16) && strspn(value, "0123456789xABCDEF") == strlen(value) && *parsed <= 0xFFFF; @@ -622,7 +622,7 @@ parse_match(struct quirks_context *ctx, s->match.version = version; } else if (streq(key, "MatchDMIModalias")) { check_set_bit(s, M_DMI); - if (!strneq(value, "dmi:", 4)) { + if (!strstartswith(value, "dmi:")) { qlog_parser(ctx, "%s: MatchDMIModalias must start with 'dmi:'\n", s->name); @@ -680,7 +680,7 @@ parse_model(struct quirks_context *ctx, bool b; enum quirk q = QUIRK_MODEL_ALPS_SERIAL_TOUCHPAD; - assert(strneq(key, "Model", 5)); + assert(strstartswith(key, "Model")); if (!parse_boolean_property(value, &b)) return false; @@ -918,11 +918,11 @@ parse_value_line(struct quirks_context *ctx, struct section *s, const char *line if (value[0] == '"' || value[0] == '\'') goto out; - if (strneq(key, "Match", 5)) + if (strstartswith(key, "Match")) rc = parse_match(ctx, s, key, value); - else if (strneq(key, "Model", 5)) + else if (strstartswith(key, "Model")) rc = parse_model(ctx, s, key, value); - else if (strneq(key, "Attr", 4)) + else if (strstartswith(key, "Attr")) rc = parse_attr(ctx, s, key, value); else qlog_error(ctx, "Unknown value prefix %s\n", line); @@ -1050,7 +1050,7 @@ parse_file(struct quirks_context *ctx, const char *path) path, lineno, line); goto out; case STATE_MATCH: - if (!strneq(line, "Match", 5)) { + if (!strstartswith(line, "Match")) { qlog_parser(ctx, "%s:%d: expected MatchFoo=bar, have %s\n", path, lineno, line); goto out; @@ -1058,11 +1058,11 @@ parse_file(struct quirks_context *ctx, const char *path) state = STATE_MATCH_OR_VALUE; break; case STATE_MATCH_OR_VALUE: - if (!strneq(line, "Match", 5)) + if (!strstartswith(line, "Match")) state = STATE_VALUE_OR_SECTION; break; case STATE_VALUE_OR_SECTION: - if (strneq(line, "Match", 5)) { + if (strstartswith(line, "Match")) { qlog_parser(ctx, "%s:%d: expected value or [Section], have %s\n", path, lineno, line); goto out; diff --git a/src/udev-seat.c b/src/udev-seat.c index fb1a4a3d..09b08963 100644 --- a/src/udev-seat.c +++ b/src/udev-seat.c @@ -183,7 +183,7 @@ udev_input_add_devices(struct udev_input *input, struct udev *udev) continue; sysname = udev_device_get_sysname(device); - if (!strneq("event", sysname, 5)) { + if (!strstartswith(sysname, "event")) { udev_device_unref(device); continue; } @@ -227,7 +227,7 @@ evdev_udev_handler(void *data) if (!action) goto out; - if (!strneq("event", udev_device_get_sysname(udev_device), 5)) + if (!strstartswith(udev_device_get_sysname(udev_device), "event")) goto out; if (streq(action, "add")) diff --git a/src/util-prop-parsers.c b/src/util-prop-parsers.c index 2fe1b1fb..fc829e99 100644 --- a/src/util-prop-parsers.c +++ b/src/util-prop-parsers.c @@ -305,7 +305,7 @@ parse_evcode_string(const char *s, int *type_out, int *code_out) { int type, code; - if (strneq(s, "EV_", 3)) { + if (strstartswith(s, "EV_")) { type = libevdev_event_type_from_name(s); if (type == -1) return false; diff --git a/test/litest-device-keyboard-all-codes.c b/test/litest-device-keyboard-all-codes.c index 3f5d0d4a..5f18311c 100644 --- a/test/litest-device-keyboard-all-codes.c +++ b/test/litest-device-keyboard-all-codes.c @@ -57,7 +57,7 @@ all_codes_create(struct litest_device *d) for (idx = 0, code = 0; code < KEY_MAX; code++) { const char *name = libevdev_event_code_get_name(EV_KEY, code); - if (name && strneq(name, "BTN_", 4)) + if (strstartswith(name, "BTN_")) continue; events[idx++] = EV_KEY; diff --git a/test/litest.c b/test/litest.c index 2831e464..41591d01 100644 --- a/test/litest.c +++ b/test/litest.c @@ -1356,7 +1356,7 @@ litest_init_device_udev_rules(struct litest_test_device *dev, FILE *f) kv = dev->udev_properties; while (kv->key) { fprintf(f, ", \\\n\tENV{%s}=\"%s\"", kv->key, kv->value); - if (strneq(kv->key, "EVDEV_ABS_", 10)) + if (strstartswith(kv->key, "EVDEV_ABS_")) need_keyboard_builtin = true; kv++; } @@ -1423,7 +1423,7 @@ open_restricted(const char *path, int flags, void *userdata) if (fd < 0) return -errno; - if (strneq(path, prefix, strlen(prefix))) { + if (strstartswith(path, prefix)) { p = zalloc(sizeof *p); p->path = safe_strdup(path); p->fd = fd; @@ -2298,7 +2298,7 @@ udev_wait_for_device_event(struct udev_monitor *udev_monitor, } udev_syspath = udev_device_get_syspath(udev_device); - if (udev_syspath && strstartswith(udev_syspath, syspath)) + if (strstartswith(udev_syspath, syspath)) break; udev_device_unref(udev_device); diff --git a/test/test-device.c b/test/test-device.c index ad102b24..9d041c0d 100644 --- a/test/test-device.c +++ b/test/test-device.c @@ -1695,8 +1695,8 @@ START_TEST(device_button_down_remove) keyname = libevdev_event_code_get_name(EV_KEY, code); if (!keyname || - !strneq(keyname, "BTN_", 4) || - strneq(keyname, "BTN_TOOL_", 9)) + !strstartswith(keyname, "BTN_") || + strstartswith(keyname, "BTN_TOOL_")) continue; if (!libevdev_has_event_code(lidev->evdev, EV_KEY, code)) diff --git a/test/test-keyboard.c b/test/test-keyboard.c index 3c36a0a3..82955f55 100644 --- a/test/test-keyboard.c +++ b/test/test-keyboard.c @@ -342,7 +342,7 @@ START_TEST(keyboard_no_buttons) continue; name = libevdev_event_code_get_name(EV_KEY, code); - if (!name || !strneq(name, "KEY_", 4)) + if (!strstartswith(name, "KEY_")) continue; litest_keyboard_key(dev, code, true); diff --git a/test/test-path.c b/test/test-path.c index 391fba6d..d050514a 100644 --- a/test/test-path.c +++ b/test/test-path.c @@ -481,7 +481,7 @@ START_TEST(path_device_sysname) litest_assert_notnull(sysname); litest_assert_int_gt(strlen(sysname), 1U); litest_assert(strchr(sysname, '/') == NULL); - litest_assert(strneq(sysname, "event", 5)); + litest_assert(strstartswith(sysname, "event")); libinput_event_destroy(ev); } diff --git a/test/test-udev.c b/test/test-udev.c index e9aab96e..98d11822 100644 --- a/test/test-udev.c +++ b/test/test-udev.c @@ -487,7 +487,7 @@ START_TEST(udev_device_sysname) litest_assert_notnull(sysname); litest_assert_int_gt(strlen(sysname), 1U); litest_assert(strchr(sysname, '/') == NULL); - litest_assert(strneq(sysname, "event", 5)); + litest_assert(strstartswith(sysname, "event")); libinput_event_destroy(ev); } diff --git a/tools/libinput-quirks.c b/tools/libinput-quirks.c index cf3be9bd..cc5de8bc 100644 --- a/tools/libinput-quirks.c +++ b/tools/libinput-quirks.c @@ -199,7 +199,7 @@ main(int argc, char **argv) goto out; path = argv[optind]; - if (strneq(path, "/sys/", 5)) { + if (strstartswith(path, "/sys/")) { device = udev_device_new_from_syspath(udev, path); } else { struct stat st; diff --git a/tools/libinput-record.c b/tools/libinput-record.c index 4a9d3010..4e2f95ca 100644 --- a/tools/libinput-record.c +++ b/tools/libinput-record.c @@ -1365,9 +1365,9 @@ print_system_header(FILE *fp) while (fgets(osrstr, sizeof(osrstr), osrelease)) { osrstr[strlen(osrstr) - 1] = '\0'; /* linebreak */ - if (!distro && strneq(osrstr, "ID=", 3)) + if (!distro && strstartswith(osrstr, "ID=")) distro = strstrip(&osrstr[3], "\"'"); - else if (!version && strneq(osrstr, "VERSION_ID=", 11)) + else if (!version && strstartswith(osrstr, "VERSION_ID=")) version = strstrip(&osrstr[11], "\"'"); if (distro && version) { @@ -1746,11 +1746,11 @@ print_udev_properties(struct record_device *dev) key = udev_list_entry_get_name(entry); - if (strneq(key, "ID_INPUT", 8) || - strneq(key, "LIBINPUT", 8) || - strneq(key, "EVDEV_ABS", 9) || - strneq(key, "MOUSE_DPI", 9) || - strneq(key, "POINTINGSTICK_", 14)) { + if (strstartswith(key, "ID_INPUT") || + strstartswith(key, "LIBINPUT") || + strstartswith(key, "EVDEV_ABS") || + strstartswith(key, "MOUSE_DPI") || + strstartswith(key, "POINTINGSTICK_")) { value = udev_list_entry_get_value(entry); iprintf(dev->fp, I_UDEV_DATA, "- %s=%s\n", key, value); } @@ -1899,7 +1899,7 @@ print_device_description(struct record_device *dev) } static int is_event_node(const struct dirent *dir) { - return strneq(dir->d_name, "event", 5); + return strstartswith(dir->d_name, "event"); } static char * @@ -2599,7 +2599,7 @@ is_char_dev(const char *path) { struct stat st; - if (strneq(path, "/dev", 4)) + if (strstartswith(path, "/dev")) return F_DEVICE; if (stat(path, &st) != 0) { diff --git a/tools/shared.c b/tools/shared.c index 327005ed..4d7fae6e 100644 --- a/tools/shared.c +++ b/tools/shared.c @@ -665,7 +665,7 @@ find_device(const char *udev_tag) continue; sysname = udev_device_get_sysname(device); - if (!strneq("event", sysname, 5)) { + if (!strstartswith("event", sysname)) { udev_device_unref(device); continue; }