From 0c60d1581f7b12ae472c786b7dfe27a1c6ec9a47 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 7 Jan 2025 13:21:56 +0100 Subject: [PATCH] Use has_prefix() instead of strncmp() throughout This is safer than hardcoded string lengths. --- sway/commands/assign.c | 2 +- sway/commands/bar/font.c | 2 +- sway/commands/bind.c | 5 ++--- sway/commands/font.c | 4 ++-- sway/commands/gesture.c | 3 +-- sway/commands/input/events.c | 5 ++--- sway/commands/mark.c | 2 +- sway/config.c | 4 ++-- sway/config/input.c | 2 +- sway/input/cursor.c | 4 ++-- sway/input/input-manager.c | 2 +- sway/input/libinput.c | 8 ++------ sway/main.c | 4 ++-- sway/tree/container.c | 22 +++++++++++----------- sway/tree/workspace.c | 2 +- swaybar/ipc.c | 5 +++-- swaybar/render.c | 6 +++--- swaybar/tray/item.c | 5 +++-- swaybar/tray/watcher.c | 6 +++--- 19 files changed, 44 insertions(+), 49 deletions(-) diff --git a/sway/commands/assign.c b/sway/commands/assign.c index bf95cf002..5bcbb1644 100644 --- a/sway/commands/assign.c +++ b/sway/commands/assign.c @@ -23,7 +23,7 @@ struct cmd_results *cmd_assign(int argc, char **argv) { --argc; ++argv; - if (strncmp(*argv, "→", strlen("→")) == 0) { + if (has_prefix(*argv, "→")) { if (argc < 2) { free(criteria); return cmd_results_new(CMD_INVALID, "Missing workspace"); diff --git a/sway/commands/bar/font.c b/sway/commands/bar/font.c index 0c074679a..51ca20ed5 100644 --- a/sway/commands/bar/font.c +++ b/sway/commands/bar/font.c @@ -11,7 +11,7 @@ struct cmd_results *bar_cmd_font(int argc, char **argv) { char *font = join_args(argv, argc); free(config->current_bar->font); - if (strncmp(font, "pango:", 6) == 0) { + if (has_prefix(font, "pango:")) { if (config->current_bar->pango_markup == PANGO_MARKUP_DEFAULT) { config->current_bar->pango_markup = true; } diff --git a/sway/commands/bind.c b/sway/commands/bind.c index 268f28553..0d9347007 100644 --- a/sway/commands/bind.c +++ b/sway/commands/bind.c @@ -367,8 +367,7 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv, } } else if (strcmp("--exclude-titlebar", argv[0]) == 0) { exclude_titlebar = true; - } else if (strncmp("--input-device=", argv[0], - strlen("--input-device=")) == 0) { + } else if (has_prefix("--input-device=", argv[0])) { free(binding->input); binding->input = strdup(argv[0] + strlen("--input-device=")); strip_quotes(binding->input); @@ -399,7 +398,7 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv, list_t *split = split_string(argv[0], "+"); for (int i = 0; i < split->length; ++i) { // Check for group - if (strncmp(split->items[i], "Group", strlen("Group")) == 0) { + if (has_prefix(split->items[i], "Group") == 0) { if (binding->group != XKB_LAYOUT_INVALID) { free_sway_binding(binding); list_free_items_and_destroy(split); diff --git a/sway/commands/font.c b/sway/commands/font.c index 9920d03eb..842e8ae61 100644 --- a/sway/commands/font.c +++ b/sway/commands/font.c @@ -13,9 +13,9 @@ struct cmd_results *cmd_font(int argc, char **argv) { char *font = join_args(argv, argc); free(config->font); - if (strncmp(font, "pango:", 6) == 0) { + if (has_prefix(font, "pango:")) { config->pango_markup = true; - config->font = strdup(font + 6); + config->font = strdup(font + strlen("pango:")); free(font); } else { config->pango_markup = false; diff --git a/sway/commands/gesture.c b/sway/commands/gesture.c index 90a20716d..e63b7ac6e 100644 --- a/sway/commands/gesture.c +++ b/sway/commands/gesture.c @@ -121,8 +121,7 @@ static struct cmd_results *cmd_bind_or_unbind_gesture(int argc, char **argv, boo binding->flags |= BINDING_EXACT; } else if (strcmp("--no-warn", argv[0]) == 0) { warn = false; - } else if (strncmp("--input-device=", argv[0], - strlen("--input-device=")) == 0) { + } else if (has_prefix("--input-device=", argv[0])) { free(binding->input); binding->input = strdup(argv[0] + strlen("--input-device=")); } else { diff --git a/sway/commands/input/events.c b/sway/commands/input/events.c index 3cea026ec..4650108a1 100644 --- a/sway/commands/input/events.c +++ b/sway/commands/input/events.c @@ -86,7 +86,7 @@ static void toggle_select_send_events_for_device(struct input_config *ic, static void toggle_send_events(int argc, char **argv) { struct input_config *ic = config->handler_context.input_config; bool wildcard = strcmp(ic->identifier, "*") == 0; - const char *type = strncmp(ic->identifier, "type:", strlen("type:")) == 0 + const char *type = has_prefix(ic->identifier, "type:") ? ic->identifier + strlen("type:") : NULL; struct sway_input_device *device = NULL; wl_list_for_each(device, &server.input->devices, link) { @@ -146,8 +146,7 @@ struct cmd_results *input_cmd_events(int argc, char **argv) { toggle_send_events(argc - 1, argv + 1); - if (strcmp(ic->identifier, "*") == 0 || - strncmp(ic->identifier, "type:", strlen("type:")) == 0) { + if (strcmp(ic->identifier, "*") == 0 || has_prefix(ic->identifier, "type:")) { // Update the device input configs and then reset the type/wildcard // config send events mode so that is does not override the device // ones. The device ones will be applied when attempting to apply diff --git a/sway/commands/mark.c b/sway/commands/mark.c index 2bfc86b30..81bf0e38b 100644 --- a/sway/commands/mark.c +++ b/sway/commands/mark.c @@ -23,7 +23,7 @@ struct cmd_results *cmd_mark(int argc, char **argv) { } bool add = false, toggle = false; - while (argc > 0 && strncmp(*argv, "--", 2) == 0) { + while (argc > 0 && has_prefix(*argv, "--") == 0) { if (strcmp(*argv, "--add") == 0) { add = true; } else if (strcmp(*argv, "--replace") == 0) { diff --git a/sway/config.c b/sway/config.c index 1090edc5c..ec7059687 100644 --- a/sway/config.c +++ b/sway/config.c @@ -925,8 +925,8 @@ char *do_var_replacement(char *str) { // Find matching variable for (i = 0; i < config->symbols->length; ++i) { struct sway_variable *var = config->symbols->items[i]; - int vnlen = strlen(var->name); - if (strncmp(find, var->name, vnlen) == 0) { + if (has_prefix(find, var->name)) { + int vnlen = strlen(var->name); int vvlen = strlen(var->value); char *newstr = malloc(strlen(str) - vnlen + vvlen + 1); if (!newstr) { diff --git a/sway/config/input.c b/sway/config/input.c index e5694effc..1fb737b65 100644 --- a/sway/config/input.c +++ b/sway/config/input.c @@ -300,7 +300,7 @@ struct input_config *store_input_config(struct input_config *ic, return NULL; } - bool type = strncmp(ic->identifier, "type:", strlen("type:")) == 0; + bool type = has_prefix(ic->identifier, "type:"); if (type && error && !validate_type_on_existing(ic, error)) { return NULL; } diff --git a/sway/input/cursor.c b/sway/input/cursor.c index bbd16717f..e8c451183 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -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; diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index ddfe14689..99af4c691 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -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; diff --git a/sway/input/libinput.c b/sway/input/libinput.c index b9df8d0bf..f76c6505c 100644 --- a/sway/input/libinput.c +++ b/sway/input/libinput.c @@ -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-"); } diff --git a/sway/main.c b/sway/main.c index 165ccc095..accffc71f 100644 --- a/sway/main.c +++ b/sway/main.c @@ -159,8 +159,8 @@ void enable_debug_flag(const char *flag) { debug.txn_wait = true; } else if (strcmp(flag, "txn-timings") == 0) { debug.txn_timings = true; - } else if (strncmp(flag, "txn-timeout=", 12) == 0) { - server.txn_timeout_ms = atoi(&flag[12]); + } else if (has_prefix(flag, "txn-timeout=")) { + server.txn_timeout_ms = atoi(&flag[strlen("txn-timeout=")]); } else if (strcmp(flag, "legacy-wl-drm") == 0) { debug.legacy_wl_drm = true; } else { diff --git a/sway/tree/container.c b/sway/tree/container.c index a04c4c36b..0288beacb 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -696,26 +696,26 @@ size_t parse_title_format(struct sway_container *container, char *buffer) { len += next - format; format = next; - if (strncmp(next, "%title", 6) == 0) { + if (has_prefix(next, "%title")) { if (container->view) { len += append_prop(buffer, view_get_title(container->view)); } else { len += container_build_representation(container->pending.layout, container->pending.children, buffer); } - format += 6; + format += strlen("%title"); } else if (container->view) { - if (strncmp(next, "%app_id", 7) == 0) { + if (has_prefix(next, "%app_id")) { len += append_prop(buffer, view_get_app_id(container->view)); - format += 7; - } else if (strncmp(next, "%class", 6) == 0) { + format += strlen("%app_id"); + } else if (has_prefix(next, "%class")) { len += append_prop(buffer, view_get_class(container->view)); - format += 6; - } else if (strncmp(next, "%instance", 9) == 0) { + format += strlen("%class"); + } else if (has_prefix(next, "%instance")) { len += append_prop(buffer, view_get_instance(container->view)); - format += 9; - } else if (strncmp(next, "%shell", 6) == 0) { + format += strlen("%instance"); + } else if (has_prefix(next, "%shell")) { len += append_prop(buffer, view_get_shell(container->view)); - format += 6; + format += strlen("%shell"); } else { lenient_strcat(buffer, "%"); ++format; @@ -778,7 +778,7 @@ size_t container_build_representation(enum sway_container_layout layout, len += strlen(identifier); lenient_strcat(buffer, identifier); } else { - len += 6; + len += strlen("(null)"); lenient_strcat(buffer, "(null)"); } } diff --git a/sway/tree/workspace.c b/sway/tree/workspace.c index f8709a4c7..a09dc3a45 100644 --- a/sway/tree/workspace.c +++ b/sway/tree/workspace.c @@ -246,7 +246,7 @@ static void workspace_name_from_binding(const struct sway_binding * binding, } // If the command is workspace number , isolate the name - if (strncmp(_target, "number ", strlen("number ")) == 0) { + if (has_prefix(_target, "number ")) { size_t length = strlen(_target) - strlen("number ") + 1; char *temp = malloc(length); strncpy(temp, _target + strlen("number "), length - 1); diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 71c9a4c5e..f651f0359 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -15,6 +15,7 @@ #include "list.h" #include "log.h" #include "loop.h" +#include "stringop.h" #include "util.h" void ipc_send_workspace_command(struct swaybar *bar, const char *ws) { @@ -45,8 +46,8 @@ void ipc_send_workspace_command(struct swaybar *bar, const char *ws) { char *parse_font(const char *font) { char *new_font = NULL; - if (strncmp("pango:", font, 6) == 0) { - font += 6; + if (has_prefix("pango:", font)) { + font += strlen("pango:"); } new_font = strdup(font); return new_font; diff --git a/swaybar/render.c b/swaybar/render.c index 13cfdc97a..45faefa97 100644 --- a/swaybar/render.c +++ b/swaybar/render.c @@ -293,11 +293,11 @@ static uint32_t render_status_block(struct render_context *ctx, } double offset = 0; - if (strncmp(block->align, "left", 4) == 0) { + if (has_prefix(block->align, "left")) { offset = x_pos; - } else if (strncmp(block->align, "right", 5) == 0) { + } else if (has_prefix(block->align, "right")) { offset = x_pos + width - text_width; - } else if (strncmp(block->align, "center", 6) == 0) { + } else if (has_prefix(block->align, "center")) { offset = x_pos + (width - text_width) / 2; } double text_y = height / 2.0 - text_height / 2.0; diff --git a/swaybar/tray/item.c b/swaybar/tray/item.c index ca6c03ad5..12929743b 100644 --- a/swaybar/tray/item.c +++ b/swaybar/tray/item.c @@ -15,6 +15,7 @@ #include "cairo_util.h" #include "list.h" #include "log.h" +#include "stringop.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" // TODO menu @@ -161,7 +162,7 @@ static int get_property_callback(sd_bus_message *msg, void *data, } if (strcmp(prop, "Status") == 0 || (sni->status && (sni->status[0] == 'N' ? - prop[0] == 'A' : strncmp(prop, "Icon", 4) == 0))) { + prop[0] == 'A' : has_prefix(prop, "Icon")))) { set_sni_dirty(sni); } cleanup: @@ -364,7 +365,7 @@ static void handle_click(struct swaybar_sni *sni, int x, int y, method = "ContextMenu"; } - if (strncmp(method, "Scroll", strlen("Scroll")) == 0) { + if (has_prefix(method, "Scroll")) { char dir = method[strlen("Scroll")]; char *orientation = (dir == 'U' || dir == 'D') ? "vertical" : "horizontal"; int sign = (dir == 'U' || dir == 'L') ? -1 : 1; diff --git a/swaybar/tray/watcher.c b/swaybar/tray/watcher.c index 54f000bd6..284964030 100644 --- a/swaybar/tray/watcher.c +++ b/swaybar/tray/watcher.c @@ -31,9 +31,9 @@ static int handle_lost_service(sd_bus_message *msg, struct swaybar_watcher *watcher = data; for (int idx = 0; idx < watcher->items->length; ++idx) { char *id = watcher->items->items[idx]; - int cmp_res = using_standard_protocol(watcher) ? - cmp_id(id, service) : strncmp(id, service, strlen(service)); - if (cmp_res == 0) { + bool cmp_res = using_standard_protocol(watcher) ? + cmp_id(id, service) == 0 : has_prefix(id, service); + if (cmp_res) { sway_log(SWAY_DEBUG, "Unregistering Status Notifier Item '%s'", id); list_del(watcher->items, idx--); sd_bus_emit_signal(watcher->bus, obj_path, watcher->interface,