Use has_prefix() instead of strncmp() throughout

This is safer than hardcoded string lengths.
This commit is contained in:
Simon Ser 2025-01-07 13:21:56 +01:00 committed by Kenny Levinsen
parent c55dff95bc
commit 0c60d1581f
19 changed files with 44 additions and 49 deletions

View file

@ -1212,7 +1212,7 @@ uint32_t get_mouse_bindsym(const char *name, char **error) {
SWAY_SCROLL_UP, SWAY_SCROLL_DOWN, SWAY_SCROLL_LEFT,
SWAY_SCROLL_RIGHT, BTN_SIDE, BTN_EXTRA};
return buttons[number - 1];
} else if (strncmp(name, "BTN_", strlen("BTN_")) == 0) {
} else if (has_prefix(name, "BTN_")) {
// Get event code from name
int code = libevdev_event_code_from_name(EV_KEY, name);
if (code == -1) {
@ -1237,7 +1237,7 @@ uint32_t get_mouse_bindcode(const char *name, char **error) {
return 0;
}
const char *event = libevdev_event_code_get_name(EV_KEY, code);
if (!event || strncmp(event, "BTN_", strlen("BTN_")) != 0) {
if (!event || !has_prefix(event, "BTN_")) {
*error = format_str("Event code %d (%s) is not a button",
code, event ? event : "(null)");
return 0;

View file

@ -577,7 +577,7 @@ void input_manager_configure_all_input_mappings(void) {
void input_manager_apply_input_config(struct input_config *input_config) {
struct sway_input_device *input_device = NULL;
bool wildcard = strcmp(input_config->identifier, "*") == 0;
bool type_wildcard = strncmp(input_config->identifier, "type:", 5) == 0;
bool type_wildcard = has_prefix(input_config->identifier, "type:");
wl_list_for_each(input_device, &server.input->devices, link) {
bool type_matches = type_wildcard &&
strcmp(input_device_get_type(input_device), input_config->identifier + 5) == 0;

View file

@ -417,13 +417,9 @@ bool sway_libinput_device_is_builtin(struct sway_input_device *sway_device) {
return false;
}
const char prefix_platform[] = "platform-";
if (strncmp(id_path, prefix_platform, strlen(prefix_platform)) == 0) {
if (has_prefix(id_path, "platform-")) {
return true;
}
const char prefix_pci[] = "pci-";
const char infix_platform[] = "-platform-";
return (strncmp(id_path, prefix_pci, strlen(prefix_pci)) == 0) &&
strstr(id_path, infix_platform);
return has_prefix(id_path, "pci-") && strstr(id_path, "-platform-");
}