From b1f0b60542620f7f19a01529872edda086f6486f Mon Sep 17 00:00:00 2001 From: Orestis Floros Date: Wed, 8 May 2024 08:00:47 +0200 Subject: [PATCH] Revert to snprintf --- include/i3status.h | 2 +- src/output.c | 36 +++++++++++++++++------------------- src/print_wireless_info.c | 13 ++++++------- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/include/i3status.h b/include/i3status.h index 1c75eaa0..ad05055d 100644 --- a/include/i3status.h +++ b/include/i3status.h @@ -204,7 +204,7 @@ void print_separator(const char *separator); char *color(const char *colorstr); char *endcolor() __attribute__((pure)); void reset_cursor(void); -char *maybe_escape_markup(char *text); +void maybe_escape_markup(char *text, char *buffer, size_t size); char *rtrim(const char *s); char *ltrim(const char *s); diff --git a/src/output.c b/src/output.c index d8c6a25b..06ba39e0 100644 --- a/src/output.c +++ b/src/output.c @@ -88,49 +88,47 @@ void reset_cursor(void) { * https://git.gnome.org/browse/glib/tree/glib/gmarkup.c?id=03db1f455b4265654e237d2ad55464b4113cba8a#n2142 * */ -char *maybe_escape_markup(char *text) { +void maybe_escape_markup(char *text, char *buffer, size_t size) { + size--; /* Leave a byte for NUL termination. */ + if (markup_format == M_NONE) { - return strdup(text); + *buffer += snprintf(buffer, size, "%s", text); + return; } - size_t idx = 0; - size_t size = 32; - char *buffer = malloc(size); - for (; *text != '\0'; text++) { - if (idx + 10 > size) { - size *= 2; - buffer = realloc(buffer, size); + for (size_t i = 0; *text != '\0'; text++) { + if (i >= size) { + /* trim buffer and leave */ + return; } switch (*text) { case '&': - idx += sprintf(&buffer[idx], "%s", "&"); + i += snprintf(&buffer[i], size - i, "%s", "&"); break; case '<': - idx += sprintf(&buffer[idx], "%s", "<"); + i += snprintf(&buffer[i], size - i, "%s", "<"); break; case '>': - idx += sprintf(&buffer[idx], "%s", ">"); + i += snprintf(&buffer[i], size - i, "%s", ">"); break; case '\'': - idx += sprintf(&buffer[idx], "%s", "'"); + i += snprintf(&buffer[i], size - i, "%s", "'"); break; case '"': - idx += sprintf(&buffer[idx], "%s", """); + i += snprintf(&buffer[i], size - i, "%s", """); break; default: if ((0x1 <= *text && *text <= 0x8) || (0xb <= *text && *text <= 0xc) || (0xe <= *text && *text <= 0x1f)) { - idx += sprintf(&buffer[idx], "&#x%x;", *text); + i += snprintf(&buffer[i], size - i, "&#x%x;", *text); } else { - buffer[idx] = *text; - idx++; + buffer[i] = *text; + i++; } break; } } - buffer[idx] = 0; - return buffer; } /* diff --git a/src/print_wireless_info.c b/src/print_wireless_info.c index cd8f494e..88d87f44 100644 --- a/src/print_wireless_info.c +++ b/src/print_wireless_info.c @@ -68,7 +68,7 @@ #include "i3status.h" -#define STRING_SIZE 30 +#define STRING_SIZE 60 #define WIRELESS_INFO_FLAG_HAS_ESSID (1 << 0) #define WIRELESS_INFO_FLAG_HAS_QUALITY (1 << 1) @@ -569,6 +569,7 @@ void print_wireless_info(wireless_info_ctx_t *ctx) { char string_quality[STRING_SIZE] = {'\0'}; char string_signal[STRING_SIZE] = {'\0'}; char string_noise[STRING_SIZE] = {'\0'}; + char string_essid[STRING_SIZE] = {'\0'}; char string_frequency[STRING_SIZE] = {'\0'}; char string_ip[STRING_SIZE] = {'\0'}; char string_bitrate[STRING_SIZE] = {'\0'}; @@ -600,13 +601,12 @@ void print_wireless_info(wireless_info_ctx_t *ctx) { snprintf(string_noise, STRING_SIZE, "?"); } - char *string_essid_tmp = NULL; /* Dynamic allocation of ESSID */ - char *string_essid = "?"; #ifdef IW_ESSID_MAX_SIZE - if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID) { - string_essid = string_essid_tmp = maybe_escape_markup(info.essid); - } + if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID) + maybe_escape_markup(info.essid, string_essid, STRING_SIZE); + else #endif + snprintf(string_essid, STRING_SIZE, "?"); if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY) snprintf(string_frequency, STRING_SIZE, "%1.1f GHz", info.frequency / 1e9); @@ -632,7 +632,6 @@ void print_wireless_info(wireless_info_ctx_t *ctx) { char *formatted = format_placeholders(walk, &placeholders[0], num); OUTPUT_FORMATTED; free(formatted); - free(string_essid_tmp); END_COLOR; free(ipv4_address);