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

@ -23,7 +23,7 @@ struct cmd_results *cmd_assign(int argc, char **argv) {
--argc; ++argv; --argc; ++argv;
if (strncmp(*argv, "", strlen("")) == 0) { if (has_prefix(*argv, "")) {
if (argc < 2) { if (argc < 2) {
free(criteria); free(criteria);
return cmd_results_new(CMD_INVALID, "Missing workspace"); return cmd_results_new(CMD_INVALID, "Missing workspace");

View file

@ -11,7 +11,7 @@ struct cmd_results *bar_cmd_font(int argc, char **argv) {
char *font = join_args(argv, argc); char *font = join_args(argv, argc);
free(config->current_bar->font); 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) { if (config->current_bar->pango_markup == PANGO_MARKUP_DEFAULT) {
config->current_bar->pango_markup = true; config->current_bar->pango_markup = true;
} }

View file

@ -367,8 +367,7 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv,
} }
} else if (strcmp("--exclude-titlebar", argv[0]) == 0) { } else if (strcmp("--exclude-titlebar", argv[0]) == 0) {
exclude_titlebar = true; exclude_titlebar = true;
} else if (strncmp("--input-device=", argv[0], } else if (has_prefix("--input-device=", argv[0])) {
strlen("--input-device=")) == 0) {
free(binding->input); free(binding->input);
binding->input = strdup(argv[0] + strlen("--input-device=")); binding->input = strdup(argv[0] + strlen("--input-device="));
strip_quotes(binding->input); 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], "+"); list_t *split = split_string(argv[0], "+");
for (int i = 0; i < split->length; ++i) { for (int i = 0; i < split->length; ++i) {
// Check for group // 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) { if (binding->group != XKB_LAYOUT_INVALID) {
free_sway_binding(binding); free_sway_binding(binding);
list_free_items_and_destroy(split); list_free_items_and_destroy(split);

View file

@ -13,9 +13,9 @@ struct cmd_results *cmd_font(int argc, char **argv) {
char *font = join_args(argv, argc); char *font = join_args(argv, argc);
free(config->font); free(config->font);
if (strncmp(font, "pango:", 6) == 0) { if (has_prefix(font, "pango:")) {
config->pango_markup = true; config->pango_markup = true;
config->font = strdup(font + 6); config->font = strdup(font + strlen("pango:"));
free(font); free(font);
} else { } else {
config->pango_markup = false; config->pango_markup = false;

View file

@ -121,8 +121,7 @@ static struct cmd_results *cmd_bind_or_unbind_gesture(int argc, char **argv, boo
binding->flags |= BINDING_EXACT; binding->flags |= BINDING_EXACT;
} else if (strcmp("--no-warn", argv[0]) == 0) { } else if (strcmp("--no-warn", argv[0]) == 0) {
warn = false; warn = false;
} else if (strncmp("--input-device=", argv[0], } else if (has_prefix("--input-device=", argv[0])) {
strlen("--input-device=")) == 0) {
free(binding->input); free(binding->input);
binding->input = strdup(argv[0] + strlen("--input-device=")); binding->input = strdup(argv[0] + strlen("--input-device="));
} else { } else {

View file

@ -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) { static void toggle_send_events(int argc, char **argv) {
struct input_config *ic = config->handler_context.input_config; struct input_config *ic = config->handler_context.input_config;
bool wildcard = strcmp(ic->identifier, "*") == 0; 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; ? ic->identifier + strlen("type:") : NULL;
struct sway_input_device *device = NULL; struct sway_input_device *device = NULL;
wl_list_for_each(device, &server.input->devices, link) { 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); toggle_send_events(argc - 1, argv + 1);
if (strcmp(ic->identifier, "*") == 0 || if (strcmp(ic->identifier, "*") == 0 || has_prefix(ic->identifier, "type:")) {
strncmp(ic->identifier, "type:", strlen("type:")) == 0) {
// Update the device input configs and then reset the type/wildcard // Update the device input configs and then reset the type/wildcard
// config send events mode so that is does not override the device // config send events mode so that is does not override the device
// ones. The device ones will be applied when attempting to apply // ones. The device ones will be applied when attempting to apply

View file

@ -23,7 +23,7 @@ struct cmd_results *cmd_mark(int argc, char **argv) {
} }
bool add = false, toggle = false; bool add = false, toggle = false;
while (argc > 0 && strncmp(*argv, "--", 2) == 0) { while (argc > 0 && has_prefix(*argv, "--") == 0) {
if (strcmp(*argv, "--add") == 0) { if (strcmp(*argv, "--add") == 0) {
add = true; add = true;
} else if (strcmp(*argv, "--replace") == 0) { } else if (strcmp(*argv, "--replace") == 0) {

View file

@ -925,8 +925,8 @@ char *do_var_replacement(char *str) {
// Find matching variable // Find matching variable
for (i = 0; i < config->symbols->length; ++i) { for (i = 0; i < config->symbols->length; ++i) {
struct sway_variable *var = config->symbols->items[i]; struct sway_variable *var = config->symbols->items[i];
if (has_prefix(find, var->name)) {
int vnlen = strlen(var->name); int vnlen = strlen(var->name);
if (strncmp(find, var->name, vnlen) == 0) {
int vvlen = strlen(var->value); int vvlen = strlen(var->value);
char *newstr = malloc(strlen(str) - vnlen + vvlen + 1); char *newstr = malloc(strlen(str) - vnlen + vvlen + 1);
if (!newstr) { if (!newstr) {

View file

@ -300,7 +300,7 @@ struct input_config *store_input_config(struct input_config *ic,
return NULL; 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)) { if (type && error && !validate_type_on_existing(ic, error)) {
return NULL; return NULL;
} }

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_UP, SWAY_SCROLL_DOWN, SWAY_SCROLL_LEFT,
SWAY_SCROLL_RIGHT, BTN_SIDE, BTN_EXTRA}; SWAY_SCROLL_RIGHT, BTN_SIDE, BTN_EXTRA};
return buttons[number - 1]; return buttons[number - 1];
} else if (strncmp(name, "BTN_", strlen("BTN_")) == 0) { } else if (has_prefix(name, "BTN_")) {
// Get event code from name // Get event code from name
int code = libevdev_event_code_from_name(EV_KEY, name); int code = libevdev_event_code_from_name(EV_KEY, name);
if (code == -1) { if (code == -1) {
@ -1237,7 +1237,7 @@ uint32_t get_mouse_bindcode(const char *name, char **error) {
return 0; return 0;
} }
const char *event = libevdev_event_code_get_name(EV_KEY, code); 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", *error = format_str("Event code %d (%s) is not a button",
code, event ? event : "(null)"); code, event ? event : "(null)");
return 0; 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) { void input_manager_apply_input_config(struct input_config *input_config) {
struct sway_input_device *input_device = NULL; struct sway_input_device *input_device = NULL;
bool wildcard = strcmp(input_config->identifier, "*") == 0; 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) { wl_list_for_each(input_device, &server.input->devices, link) {
bool type_matches = type_wildcard && bool type_matches = type_wildcard &&
strcmp(input_device_get_type(input_device), input_config->identifier + 5) == 0; 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; return false;
} }
const char prefix_platform[] = "platform-"; if (has_prefix(id_path, "platform-")) {
if (strncmp(id_path, prefix_platform, strlen(prefix_platform)) == 0) {
return true; return true;
} }
const char prefix_pci[] = "pci-"; return has_prefix(id_path, "pci-") && strstr(id_path, "-platform-");
const char infix_platform[] = "-platform-";
return (strncmp(id_path, prefix_pci, strlen(prefix_pci)) == 0) &&
strstr(id_path, infix_platform);
} }

View file

@ -159,8 +159,8 @@ void enable_debug_flag(const char *flag) {
debug.txn_wait = true; debug.txn_wait = true;
} else if (strcmp(flag, "txn-timings") == 0) { } else if (strcmp(flag, "txn-timings") == 0) {
debug.txn_timings = true; debug.txn_timings = true;
} else if (strncmp(flag, "txn-timeout=", 12) == 0) { } else if (has_prefix(flag, "txn-timeout=")) {
server.txn_timeout_ms = atoi(&flag[12]); server.txn_timeout_ms = atoi(&flag[strlen("txn-timeout=")]);
} else if (strcmp(flag, "legacy-wl-drm") == 0) { } else if (strcmp(flag, "legacy-wl-drm") == 0) {
debug.legacy_wl_drm = true; debug.legacy_wl_drm = true;
} else { } else {

View file

@ -696,26 +696,26 @@ size_t parse_title_format(struct sway_container *container, char *buffer) {
len += next - format; len += next - format;
format = next; format = next;
if (strncmp(next, "%title", 6) == 0) { if (has_prefix(next, "%title")) {
if (container->view) { if (container->view) {
len += append_prop(buffer, view_get_title(container->view)); len += append_prop(buffer, view_get_title(container->view));
} else { } else {
len += container_build_representation(container->pending.layout, container->pending.children, buffer); len += container_build_representation(container->pending.layout, container->pending.children, buffer);
} }
format += 6; format += strlen("%title");
} else if (container->view) { } 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)); len += append_prop(buffer, view_get_app_id(container->view));
format += 7; format += strlen("%app_id");
} else if (strncmp(next, "%class", 6) == 0) { } else if (has_prefix(next, "%class")) {
len += append_prop(buffer, view_get_class(container->view)); len += append_prop(buffer, view_get_class(container->view));
format += 6; format += strlen("%class");
} else if (strncmp(next, "%instance", 9) == 0) { } else if (has_prefix(next, "%instance")) {
len += append_prop(buffer, view_get_instance(container->view)); len += append_prop(buffer, view_get_instance(container->view));
format += 9; format += strlen("%instance");
} else if (strncmp(next, "%shell", 6) == 0) { } else if (has_prefix(next, "%shell")) {
len += append_prop(buffer, view_get_shell(container->view)); len += append_prop(buffer, view_get_shell(container->view));
format += 6; format += strlen("%shell");
} else { } else {
lenient_strcat(buffer, "%"); lenient_strcat(buffer, "%");
++format; ++format;
@ -778,7 +778,7 @@ size_t container_build_representation(enum sway_container_layout layout,
len += strlen(identifier); len += strlen(identifier);
lenient_strcat(buffer, identifier); lenient_strcat(buffer, identifier);
} else { } else {
len += 6; len += strlen("(null)");
lenient_strcat(buffer, "(null)"); lenient_strcat(buffer, "(null)");
} }
} }

View file

@ -246,7 +246,7 @@ static void workspace_name_from_binding(const struct sway_binding * binding,
} }
// If the command is workspace number <name>, isolate the name // If the command is workspace number <name>, isolate the name
if (strncmp(_target, "number ", strlen("number ")) == 0) { if (has_prefix(_target, "number ")) {
size_t length = strlen(_target) - strlen("number ") + 1; size_t length = strlen(_target) - strlen("number ") + 1;
char *temp = malloc(length); char *temp = malloc(length);
strncpy(temp, _target + strlen("number "), length - 1); strncpy(temp, _target + strlen("number "), length - 1);

View file

@ -15,6 +15,7 @@
#include "list.h" #include "list.h"
#include "log.h" #include "log.h"
#include "loop.h" #include "loop.h"
#include "stringop.h"
#include "util.h" #include "util.h"
void ipc_send_workspace_command(struct swaybar *bar, const char *ws) { 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 *parse_font(const char *font) {
char *new_font = NULL; char *new_font = NULL;
if (strncmp("pango:", font, 6) == 0) { if (has_prefix("pango:", font)) {
font += 6; font += strlen("pango:");
} }
new_font = strdup(font); new_font = strdup(font);
return new_font; return new_font;

View file

@ -293,11 +293,11 @@ static uint32_t render_status_block(struct render_context *ctx,
} }
double offset = 0; double offset = 0;
if (strncmp(block->align, "left", 4) == 0) { if (has_prefix(block->align, "left")) {
offset = x_pos; offset = x_pos;
} else if (strncmp(block->align, "right", 5) == 0) { } else if (has_prefix(block->align, "right")) {
offset = x_pos + width - text_width; 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; offset = x_pos + (width - text_width) / 2;
} }
double text_y = height / 2.0 - text_height / 2.0; double text_y = height / 2.0 - text_height / 2.0;

View file

@ -15,6 +15,7 @@
#include "cairo_util.h" #include "cairo_util.h"
#include "list.h" #include "list.h"
#include "log.h" #include "log.h"
#include "stringop.h"
#include "wlr-layer-shell-unstable-v1-client-protocol.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h"
// TODO menu // 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' ? 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); set_sni_dirty(sni);
} }
cleanup: cleanup:
@ -364,7 +365,7 @@ static void handle_click(struct swaybar_sni *sni, int x, int y,
method = "ContextMenu"; method = "ContextMenu";
} }
if (strncmp(method, "Scroll", strlen("Scroll")) == 0) { if (has_prefix(method, "Scroll")) {
char dir = method[strlen("Scroll")]; char dir = method[strlen("Scroll")];
char *orientation = (dir == 'U' || dir == 'D') ? "vertical" : "horizontal"; char *orientation = (dir == 'U' || dir == 'D') ? "vertical" : "horizontal";
int sign = (dir == 'U' || dir == 'L') ? -1 : 1; int sign = (dir == 'U' || dir == 'L') ? -1 : 1;

View file

@ -31,9 +31,9 @@ static int handle_lost_service(sd_bus_message *msg,
struct swaybar_watcher *watcher = data; struct swaybar_watcher *watcher = data;
for (int idx = 0; idx < watcher->items->length; ++idx) { for (int idx = 0; idx < watcher->items->length; ++idx) {
char *id = watcher->items->items[idx]; char *id = watcher->items->items[idx];
int cmp_res = using_standard_protocol(watcher) ? bool cmp_res = using_standard_protocol(watcher) ?
cmp_id(id, service) : strncmp(id, service, strlen(service)); cmp_id(id, service) == 0 : has_prefix(id, service);
if (cmp_res == 0) { if (cmp_res) {
sway_log(SWAY_DEBUG, "Unregistering Status Notifier Item '%s'", id); sway_log(SWAY_DEBUG, "Unregistering Status Notifier Item '%s'", id);
list_del(watcher->items, idx--); list_del(watcher->items, idx--);
sd_bus_emit_signal(watcher->bus, obj_path, watcher->interface, sd_bus_emit_signal(watcher->bus, obj_path, watcher->interface,