From 08c1946d71039e583696842c3558b337aede1cbf Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 28 Feb 2023 16:43:05 +0100 Subject: [PATCH] Use format_str() throughout --- common/pango.c | 20 ++++---------------- sway/commands.c | 12 +----------- sway/commands/bar.c | 4 +--- sway/config.c | 15 +++++---------- sway/config/output.c | 9 ++------- sway/input/cursor.c | 15 +++------------ sway/input/input-manager.c | 10 +--------- sway/input/keyboard.c | 29 ++++++----------------------- sway/swaynag.c | 14 ++++---------- swaybar/tray/host.c | 10 +++------- swaybar/tray/icon.c | 26 ++++++-------------------- swaybar/tray/watcher.c | 9 +++------ 12 files changed, 39 insertions(+), 134 deletions(-) diff --git a/common/pango.c b/common/pango.c index e04bf80f..288569b3 100644 --- a/common/pango.c +++ b/common/pango.c @@ -84,18 +84,11 @@ void get_text_size(cairo_t *cairo, const PangoFontDescription *desc, int *width, int *baseline, double scale, bool markup, const char *fmt, ...) { va_list args; va_start(args, fmt); - // Add one since vsnprintf excludes null terminator. - int length = vsnprintf(NULL, 0, fmt, args) + 1; + char *buf = vformat_str(fmt, args); va_end(args); - - char *buf = malloc(length); if (buf == NULL) { - sway_log(SWAY_ERROR, "Failed to allocate memory"); return; } - va_start(args, fmt); - vsnprintf(buf, length, fmt, args); - va_end(args); PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup); pango_cairo_update_layout(cairo, layout); @@ -104,6 +97,7 @@ void get_text_size(cairo_t *cairo, const PangoFontDescription *desc, int *width, *baseline = pango_layout_get_baseline(layout) / PANGO_SCALE; } g_object_unref(layout); + free(buf); } @@ -125,18 +119,11 @@ void render_text(cairo_t *cairo, const PangoFontDescription *desc, double scale, bool markup, const char *fmt, ...) { va_list args; va_start(args, fmt); - // Add one since vsnprintf excludes null terminator. - int length = vsnprintf(NULL, 0, fmt, args) + 1; + char *buf = vformat_str(fmt, args); va_end(args); - - char *buf = malloc(length); if (buf == NULL) { - sway_log(SWAY_ERROR, "Failed to allocate memory"); return; } - va_start(args, fmt); - vsnprintf(buf, length, fmt, args); - va_end(args); PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup); cairo_font_options_t *fo = cairo_font_options_create(); @@ -146,5 +133,6 @@ void render_text(cairo_t *cairo, const PangoFontDescription *desc, pango_cairo_update_layout(cairo, layout); pango_cairo_show_layout(cairo, layout); g_object_unref(layout); + free(buf); } diff --git a/sway/commands.c b/sway/commands.c index 0ea44129..55eda183 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -489,20 +489,10 @@ struct cmd_results *cmd_results_new(enum cmd_status status, } results->status = status; if (format) { - char *error = NULL; va_list args; va_start(args, format); - int slen = vsnprintf(NULL, 0, format, args); + results->error = vformat_str(format, args); va_end(args); - if (slen > 0) { - error = malloc(slen + 1); - if (error != NULL) { - va_start(args, format); - vsnprintf(error, slen + 1, format, args); - va_end(args); - } - } - results->error = error; } else { results->error = NULL; } diff --git a/sway/commands/bar.c b/sway/commands/bar.c index 8571d282..22756acb 100644 --- a/sway/commands/bar.c +++ b/sway/commands/bar.c @@ -73,12 +73,10 @@ struct cmd_results *cmd_bar(int argc, char **argv) { } ++argv; --argc; } else if (config->reading && !config->current_bar) { - int len = snprintf(NULL, 0, "bar-%d", config->bars->length) + 1; - id = malloc(len * sizeof(char)); + id = format_str("bar-%d", config->bars->length); if (!id) { return cmd_results_new(CMD_FAILURE, "Unable to allocate bar id"); } - snprintf(id, len, "bar-%d", config->bars->length); } else if (!config->reading && strcmp(argv[0], "mode") != 0 && strcmp(argv[0], "hidden_state") != 0) { if (is_subcommand(argv[0])) { diff --git a/sway/config.c b/sway/config.c index f5efa98a..8c8c148d 100644 --- a/sway/config.c +++ b/sway/config.c @@ -924,23 +924,18 @@ void config_add_swaynag_warning(char *fmt, ...) { if (config->reading && !config->validating) { va_list args; va_start(args, fmt); - size_t length = vsnprintf(NULL, 0, fmt, args) + 1; + char *str = vformat_str(fmt, args); va_end(args); - - char *temp = malloc(length + 1); - if (!temp) { - sway_log(SWAY_ERROR, "Failed to allocate buffer for warning."); + if (str == NULL) { return; } - va_start(args, fmt); - vsnprintf(temp, length, fmt, args); - va_end(args); - swaynag_log(config->swaynag_command, &config->swaynag_config_errors, "Warning on line %i (%s) '%s': %s", config->current_config_line_number, config->current_config_path, - config->current_config_line, temp); + config->current_config_line, str); + + free(str); } } diff --git a/sway/config/output.c b/sway/config/output.c index 45d2441b..6fb29ded 100644 --- a/sway/config/output.c +++ b/sway/config/output.c @@ -162,13 +162,10 @@ static void merge_id_on_name(struct output_config *oc) { char id[128]; output_get_identifier(id, sizeof(id), output); - size_t size = snprintf(NULL, 0, "%s on %s", id, name) + 1; - char *id_on_name = malloc(size); + char *id_on_name = format_str("%s on %s", id, name); if (!id_on_name) { - sway_log(SWAY_ERROR, "Failed to allocate id on name string"); return; } - snprintf(id_on_name, size, "%s on %s", id, name); int i = list_seq_find(config->output_configs, output_name_cmp, id_on_name); if (i >= 0) { @@ -633,9 +630,7 @@ static struct output_config *get_output_config(char *identifier, struct output_config *oc_name = NULL; struct output_config *oc_id = NULL; - size_t length = snprintf(NULL, 0, "%s on %s", identifier, name) + 1; - char *id_on_name = malloc(length); - snprintf(id_on_name, length, "%s on %s", identifier, name); + char *id_on_name = format_str("%s on %s", identifier, name); int i = list_seq_find(config->output_configs, output_name_cmp, id_on_name); if (i >= 0) { oc_id_on_name = config->output_configs->items[i]; diff --git a/sway/input/cursor.c b/sway/input/cursor.c index 15687993..75d055cd 100644 --- a/sway/input/cursor.c +++ b/sway/input/cursor.c @@ -1273,11 +1273,7 @@ uint32_t get_mouse_bindsym(const char *name, char **error) { // Get event code from name int code = libevdev_event_code_from_name(EV_KEY, name); if (code == -1) { - size_t len = snprintf(NULL, 0, "Unknown event %s", name) + 1; - *error = malloc(len); - if (*error) { - snprintf(*error, len, "Unknown event %s", name); - } + *error = format_str("Unknown event %s", name); return 0; } return code; @@ -1299,13 +1295,8 @@ uint32_t get_mouse_bindcode(const char *name, char **error) { } const char *event = libevdev_event_code_get_name(EV_KEY, code); if (!event || strncmp(event, "BTN_", strlen("BTN_")) != 0) { - size_t len = snprintf(NULL, 0, "Event code %d (%s) is not a button", - code, event ? event : "(null)") + 1; - *error = malloc(len); - if (*error) { - snprintf(*error, len, "Event code %d (%s) is not a button", - code, event ? event : "(null)"); - } + *error = format_str("Event code %d (%s) is not a button", + code, event ? event : "(null)"); return 0; } return code; diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index ea2cc038..1115ba5e 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -80,15 +80,7 @@ char *input_device_get_identifier(struct wlr_input_device *device) { } } - const char *fmt = "%d:%d:%s"; - int len = snprintf(NULL, 0, fmt, vendor, product, name) + 1; - char *identifier = malloc(len); - if (!identifier) { - sway_log(SWAY_ERROR, "Unable to allocate unique input device name"); - return NULL; - } - - snprintf(identifier, len, fmt, vendor, product, name); + char *identifier = format_str("%d:%d:%s", vendor, product, name); free(name); return identifier; } diff --git a/sway/input/keyboard.c b/sway/input/keyboard.c index 45a588ec..c3bf4fbb 100644 --- a/sway/input/keyboard.c +++ b/sway/input/keyboard.c @@ -717,23 +717,11 @@ struct sway_keyboard *sway_keyboard_create(struct sway_seat *seat, static void handle_xkb_context_log(struct xkb_context *context, enum xkb_log_level level, const char *format, va_list args) { - va_list args_copy; - va_copy(args_copy, args); - size_t length = vsnprintf(NULL, 0, format, args_copy) + 1; - va_end(args_copy); - - char *error = malloc(length); - if (!error) { - sway_log(SWAY_ERROR, "Failed to allocate libxkbcommon log message"); - return; - } + char *error = vformat_str(format, args); - va_copy(args_copy, args); - vsnprintf(error, length, format, args_copy); - va_end(args_copy); - - if (error[length - 2] == '\n') { - error[length - 2] = '\0'; + size_t len = strlen(error); + if (error[len - 1] == '\n') { + error[len - 1] = '\0'; } sway_log_importance_t importance = SWAY_DEBUG; @@ -768,13 +756,8 @@ struct xkb_keymap *sway_keyboard_compile_keymap(struct input_config *ic, if (!keymap_file) { sway_log_errno(SWAY_ERROR, "cannot read xkb file %s", ic->xkb_file); if (error) { - size_t len = snprintf(NULL, 0, "cannot read xkb file %s: %s", - ic->xkb_file, strerror(errno)) + 1; - *error = malloc(len); - if (*error) { - snprintf(*error, len, "cannot read xkb_file %s: %s", - ic->xkb_file, strerror(errno)); - } + *error = format_str("cannot read xkb file %s: %s", + ic->xkb_file, strerror(errno)); } goto cleanup; } diff --git a/sway/swaynag.c b/sway/swaynag.c index 4a0a6d30..6031174d 100644 --- a/sway/swaynag.c +++ b/sway/swaynag.c @@ -145,22 +145,16 @@ void swaynag_log(const char *swaynag_command, struct swaynag_instance *swaynag, va_list args; va_start(args, fmt); - size_t length = vsnprintf(NULL, 0, fmt, args) + 1; + char *str = vformat_str(fmt, args); va_end(args); - - char *temp = malloc(length + 1); - if (!temp) { + if (!str) { sway_log(SWAY_ERROR, "Failed to allocate buffer for swaynag log entry."); return; } - va_start(args, fmt); - vsnprintf(temp, length, fmt, args); - va_end(args); - - write(swaynag->fd[1], temp, length); + write(swaynag->fd[1], str, strlen(str)); - free(temp); + free(str); } void swaynag_show(struct swaynag_instance *swaynag) { diff --git a/swaybar/tray/host.c b/swaybar/tray/host.c index ddf2416d..eea2caa5 100644 --- a/swaybar/tray/host.c +++ b/swaybar/tray/host.c @@ -10,6 +10,7 @@ #include "swaybar/tray/tray.h" #include "list.h" #include "log.h" +#include "stringop.h" static const char *watcher_path = "/StatusNotifierWatcher"; @@ -138,12 +139,10 @@ static int handle_new_watcher(sd_bus_message *msg, bool init_host(struct swaybar_host *host, char *protocol, struct swaybar_tray *tray) { - size_t len = snprintf(NULL, 0, "org.%s.StatusNotifierWatcher", protocol) + 1; - host->watcher_interface = malloc(len); + host->watcher_interface = format_str("org.%s.StatusNotifierWatcher", protocol); if (!host->watcher_interface) { return false; } - snprintf(host->watcher_interface, len, "org.%s.StatusNotifierWatcher", protocol); sd_bus_slot *reg_slot = NULL, *unreg_slot = NULL, *watcher_slot = NULL; int ret = sd_bus_match_signal(tray->bus, ®_slot, host->watcher_interface, @@ -173,13 +172,10 @@ bool init_host(struct swaybar_host *host, char *protocol, } pid_t pid = getpid(); - size_t service_len = snprintf(NULL, 0, "org.%s.StatusNotifierHost-%d", - protocol, pid) + 1; - host->service = malloc(service_len); + host->service = format_str("org.%s.StatusNotifierHost-%d", protocol, pid); if (!host->service) { goto error; } - snprintf(host->service, service_len, "org.%s.StatusNotifierHost-%d", protocol, pid); ret = sd_bus_request_name(tray->bus, host->service, 0); if (ret < 0) { sway_log(SWAY_DEBUG, "Failed to acquire service name: %s", strerror(-ret)); diff --git a/swaybar/tray/icon.c b/swaybar/tray/icon.c index c426c3d4..b513dca5 100644 --- a/swaybar/tray/icon.c +++ b/swaybar/tray/icon.c @@ -40,9 +40,7 @@ static list_t *get_basedirs(void) { data_dirs = strdup(data_dirs); char *dir = strtok(data_dirs, ":"); do { - size_t path_len = snprintf(NULL, 0, "%s/icons", dir) + 1; - char *path = malloc(path_len); - snprintf(path, path_len, "%s/icons", dir); + char *path = format_str("%s/icons", dir); list_add(basedirs, path); } while ((dir = strtok(NULL, ":"))); free(data_dirs); @@ -206,13 +204,7 @@ static const char *entry_handler(char *group, char *key, char *value, */ static struct icon_theme *read_theme_file(char *basedir, char *theme_name) { // look for index.theme file - size_t path_len = snprintf(NULL, 0, "%s/%s/index.theme", basedir, - theme_name) + 1; - char *path = malloc(path_len); - if (!path) { - return NULL; - } - snprintf(path, path_len, "%s/%s/index.theme", basedir, theme_name); + char *path = format_str("%s/%s/index.theme", basedir, theme_name); FILE *theme_file = fopen(path, "r"); free(path); if (!theme_file) { @@ -416,26 +408,20 @@ static char *find_icon_in_subdir(char *name, char *basedir, char *theme, #endif }; - size_t path_len = snprintf(NULL, 0, "%s/%s/%s/%s.EXT", basedir, theme, - subdir, name) + 1; - char *path = malloc(path_len); - for (size_t i = 0; i < sizeof(extensions) / sizeof(*extensions); ++i) { - snprintf(path, path_len, "%s/%s/%s/%s.%s", basedir, theme, subdir, - name, extensions[i]); + char *path = format_str("%s/%s/%s/%s.%s", + basedir, theme, subdir, name, extensions[i]); if (access(path, R_OK) == 0) { return path; } + free(path); } - free(path); return NULL; } static bool theme_exists_in_basedir(char *theme, char *basedir) { - size_t path_len = snprintf(NULL, 0, "%s/%s", basedir, theme) + 1; - char *path = malloc(path_len); - snprintf(path, path_len, "%s/%s", basedir, theme); + char *path = format_str("%s/%s", basedir, theme); bool ret = dir_exists(path); free(path); return ret; diff --git a/swaybar/tray/watcher.c b/swaybar/tray/watcher.c index 16afc27c..551e1d12 100644 --- a/swaybar/tray/watcher.c +++ b/swaybar/tray/watcher.c @@ -6,6 +6,7 @@ #include #include "list.h" #include "log.h" +#include "stringop.h" #include "swaybar/tray/watcher.h" static const char *obj_path = "/StatusNotifierWatcher"; @@ -76,9 +77,7 @@ static int register_sni(sd_bus_message *msg, void *data, sd_bus_error *error) { service = service_or_path; path = "/StatusNotifierItem"; } - size_t id_len = snprintf(NULL, 0, "%s%s", service, path) + 1; - id = malloc(id_len); - snprintf(id, id_len, "%s%s", service, path); + id = format_str("%s%s", service, path); } if (list_seq_find(watcher->items, cmp_id, id) == -1) { @@ -159,9 +158,7 @@ struct swaybar_watcher *create_watcher(char *protocol, sd_bus *bus) { return NULL; } - size_t len = snprintf(NULL, 0, "org.%s.StatusNotifierWatcher", protocol) + 1; - watcher->interface = malloc(len); - snprintf(watcher->interface, len, "org.%s.StatusNotifierWatcher", protocol); + watcher->interface = format_str("org.%s.StatusNotifierWatcher", protocol); sd_bus_slot *signal_slot = NULL, *vtable_slot = NULL; int ret = sd_bus_add_object_vtable(bus, &vtable_slot, obj_path,