Skip to content

Commit 2e4812a

Browse files
committed
Revert to snprintf
1 parent 6efb409 commit 2e4812a

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

include/i3status.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ void print_separator(const char *separator);
204204
char *color(const char *colorstr);
205205
char *endcolor() __attribute__((pure));
206206
void reset_cursor(void);
207-
char *maybe_escape_markup(char *text);
207+
void maybe_escape_markup(char *text, char *buffer, size_t size);
208208

209209
char *rtrim(const char *s);
210210
char *ltrim(const char *s);

src/output.c

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,49 +88,46 @@ void reset_cursor(void) {
8888
* https://git.gnome.org/browse/glib/tree/glib/gmarkup.c?id=03db1f455b4265654e237d2ad55464b4113cba8a#n2142
8989
*
9090
*/
91-
char *maybe_escape_markup(char *text) {
91+
void maybe_escape_markup(char *text, char *buffer, size_t size) {
92+
size--; /* Leave a byte for NUL termination. */
93+
9294
if (markup_format == M_NONE) {
93-
return strdup(text);
95+
*buffer += snprintf(buffer, size, "%s", text);
96+
return;
9497
}
9598

96-
size_t idx = 0;
97-
size_t size = 32;
98-
char *buffer = malloc(size);
99-
for (; *text != '\0'; text++) {
100-
if (idx + 10 > size) {
101-
size *= 2;
102-
buffer = realloc(buffer, size);
99+
for (size_t i = 0; *text != '\0'; text++) {
100+
if (i >= size) {
101+
return;
103102
}
104103
switch (*text) {
105104
case '&':
106-
idx += sprintf(&buffer[idx], "%s", "&");
105+
i += snprintf(&buffer[i], size - i, "%s", "&");
107106
break;
108107
case '<':
109-
idx += sprintf(&buffer[idx], "%s", "&lt;");
108+
i += snprintf(&buffer[i], size - i, "%s", "&lt;");
110109
break;
111110
case '>':
112-
idx += sprintf(&buffer[idx], "%s", "&gt;");
111+
i += snprintf(&buffer[i], size - i, "%s", "&gt;");
113112
break;
114113
case '\'':
115-
idx += sprintf(&buffer[idx], "%s", "&apos;");
114+
i += snprintf(&buffer[i], size - i, "%s", "&apos;");
116115
break;
117116
case '"':
118-
idx += sprintf(&buffer[idx], "%s", "&quot;");
117+
i += snprintf(&buffer[i], size - i, "%s", "&quot;");
119118
break;
120119
default:
121120
if ((0x1 <= *text && *text <= 0x8) ||
122121
(0xb <= *text && *text <= 0xc) ||
123122
(0xe <= *text && *text <= 0x1f)) {
124-
idx += sprintf(&buffer[idx], "&#x%x;", *text);
123+
i += snprintf(&buffer[i], size - i, "&#x%x;", *text);
125124
} else {
126-
buffer[idx] = *text;
127-
idx++;
125+
buffer[i] = *text;
126+
i++;
128127
}
129128
break;
130129
}
131130
}
132-
buffer[idx] = 0;
133-
return buffer;
134131
}
135132

136133
/*

src/print_wireless_info.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868

6969
#include "i3status.h"
7070

71-
#define STRING_SIZE 30
71+
#define STRING_SIZE 60
7272

7373
#define WIRELESS_INFO_FLAG_HAS_ESSID (1 << 0)
7474
#define WIRELESS_INFO_FLAG_HAS_QUALITY (1 << 1)
@@ -569,6 +569,7 @@ void print_wireless_info(wireless_info_ctx_t *ctx) {
569569
char string_quality[STRING_SIZE] = {'\0'};
570570
char string_signal[STRING_SIZE] = {'\0'};
571571
char string_noise[STRING_SIZE] = {'\0'};
572+
char string_essid[STRING_SIZE] = {'\0'};
572573
char string_frequency[STRING_SIZE] = {'\0'};
573574
char string_ip[STRING_SIZE] = {'\0'};
574575
char string_bitrate[STRING_SIZE] = {'\0'};
@@ -600,13 +601,12 @@ void print_wireless_info(wireless_info_ctx_t *ctx) {
600601
snprintf(string_noise, STRING_SIZE, "?");
601602
}
602603

603-
char *string_essid_tmp = NULL; /* Dynamic allocation of ESSID */
604-
char *string_essid = "?";
605604
#ifdef IW_ESSID_MAX_SIZE
606-
if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID) {
607-
string_essid = string_essid_tmp = maybe_escape_markup(info.essid);
608-
}
605+
if (info.flags & WIRELESS_INFO_FLAG_HAS_ESSID)
606+
maybe_escape_markup(info.essid, string_essid, STRING_SIZE);
607+
else
609608
#endif
609+
snprintf(string_essid, STRING_SIZE, "?");
610610

611611
if (info.flags & WIRELESS_INFO_FLAG_HAS_FREQUENCY)
612612
snprintf(string_frequency, STRING_SIZE, "%1.1f GHz", info.frequency / 1e9);
@@ -632,7 +632,6 @@ void print_wireless_info(wireless_info_ctx_t *ctx) {
632632
char *formatted = format_placeholders(walk, &placeholders[0], num);
633633
OUTPUT_FORMATTED;
634634
free(formatted);
635-
free(string_essid_tmp);
636635

637636
END_COLOR;
638637
free(ipv4_address);

0 commit comments

Comments
 (0)