aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorLibravatar Connor E <38229097+c-edw@users.noreply.github.com>2019-01-16 09:57:51 +0000
committerLibravatar emersion <contact@emersion.fr>2019-01-16 13:02:26 +0100
commitde6f5b345380e80b4d59ebc569697683af064424 (patch)
tree2ba6738b30a3cdcbceb0d8307424217fca448252 /common
parentRemove usage of VLAs. (diff)
downloadsway-de6f5b345380e80b4d59ebc569697683af064424.tar.gz
sway-de6f5b345380e80b4d59ebc569697683af064424.tar.zst
sway-de6f5b345380e80b4d59ebc569697683af064424.zip
Use static arrays where possible.
Diffstat (limited to 'common')
-rw-r--r--common/ipc-client.c16
-rw-r--r--common/pango.c15
2 files changed, 13 insertions, 18 deletions
diff --git a/common/ipc-client.c b/common/ipc-client.c
index 13fd8a05..1e88e71f 100644
--- a/common/ipc-client.c
+++ b/common/ipc-client.c
@@ -10,7 +10,8 @@
10#include "log.h" 10#include "log.h"
11 11
12static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; 12static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
13static const size_t ipc_header_size = sizeof(ipc_magic)+8; 13
14#define IPC_HEADER_SIZE (sizeof(ipc_magic) + 8)
14 15
15char *get_socketpath(void) { 16char *get_socketpath(void) {
16 const char *swaysock = getenv("SWAYSOCK"); 17 const char *swaysock = getenv("SWAYSOCK");
@@ -61,12 +62,12 @@ int ipc_open_socket(const char *socket_path) {
61} 62}
62 63
63struct ipc_response *ipc_recv_response(int socketfd) { 64struct ipc_response *ipc_recv_response(int socketfd) {
64 char *data = malloc(sizeof(char) * ipc_header_size); 65 char data[IPC_HEADER_SIZE];
65 uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); 66 uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
66 67
67 size_t total = 0; 68 size_t total = 0;
68 while (total < ipc_header_size) { 69 while (total < IPC_HEADER_SIZE) {
69 ssize_t received = recv(socketfd, data + total, ipc_header_size - total, 0); 70 ssize_t received = recv(socketfd, data + total, IPC_HEADER_SIZE - total, 0);
70 if (received <= 0) { 71 if (received <= 0) {
71 sway_abort("Unable to receive IPC response"); 72 sway_abort("Unable to receive IPC response");
72 } 73 }
@@ -81,7 +82,6 @@ struct ipc_response *ipc_recv_response(int socketfd) {
81 total = 0; 82 total = 0;
82 memcpy(&response->size, &data32[0], sizeof(data32[0])); 83 memcpy(&response->size, &data32[0], sizeof(data32[0]));
83 memcpy(&response->type, &data32[1], sizeof(data32[1])); 84 memcpy(&response->type, &data32[1], sizeof(data32[1]));
84 free(data);
85 85
86 char *payload = malloc(response->size + 1); 86 char *payload = malloc(response->size + 1);
87 if (!payload) { 87 if (!payload) {
@@ -113,18 +113,16 @@ void free_ipc_response(struct ipc_response *response) {
113} 113}
114 114
115char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) { 115char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) {
116 char *data = malloc(sizeof(char) * ipc_header_size); 116 char data[IPC_HEADER_SIZE];
117 uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); 117 uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
118 memcpy(data, ipc_magic, sizeof(ipc_magic)); 118 memcpy(data, ipc_magic, sizeof(ipc_magic));
119 memcpy(&data32[0], len, sizeof(*len)); 119 memcpy(&data32[0], len, sizeof(*len));
120 memcpy(&data32[1], &type, sizeof(type)); 120 memcpy(&data32[1], &type, sizeof(type));
121 121
122 if (write(socketfd, data, ipc_header_size) == -1) { 122 if (write(socketfd, data, IPC_HEADER_SIZE) == -1) {
123 sway_abort("Unable to send IPC header"); 123 sway_abort("Unable to send IPC header");
124 } 124 }
125 125
126 free(data);
127
128 if (write(socketfd, payload, *len) == -1) { 126 if (write(socketfd, payload, *len) == -1) {
129 sway_abort("Unable to send IPC payload"); 127 sway_abort("Unable to send IPC payload");
130 } 128 }
diff --git a/common/pango.c b/common/pango.c
index 18b92e9d..db8413f7 100644
--- a/common/pango.c
+++ b/common/pango.c
@@ -10,8 +10,9 @@
10#include "log.h" 10#include "log.h"
11#include "stringop.h" 11#include "stringop.h"
12 12
13#define MAX_CHARS 16384
14
13static const char overflow[] = "[buffer overflow]"; 15static const char overflow[] = "[buffer overflow]";
14static const int max_chars = 16384;
15 16
16size_t escape_markup_text(const char *src, char *dest) { 17size_t escape_markup_text(const char *src, char *dest) {
17 size_t length = 0; 18 size_t length = 0;
@@ -87,11 +88,11 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font,
87 88
88void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, 89void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
89 int *baseline, double scale, bool markup, const char *fmt, ...) { 90 int *baseline, double scale, bool markup, const char *fmt, ...) {
90 char *buf = malloc(sizeof(char) * max_chars); 91 char buf[MAX_CHARS];
91 92
92 va_list args; 93 va_list args;
93 va_start(args, fmt); 94 va_start(args, fmt);
94 if (vsnprintf(buf, sizeof(char) * max_chars, fmt, args) >= max_chars) { 95 if (vsnprintf(buf, sizeof(buf), fmt, args) >= MAX_CHARS) {
95 strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow); 96 strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow);
96 } 97 }
97 va_end(args); 98 va_end(args);
@@ -103,17 +104,15 @@ void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
103 *baseline = pango_layout_get_baseline(layout) / PANGO_SCALE; 104 *baseline = pango_layout_get_baseline(layout) / PANGO_SCALE;
104 } 105 }
105 g_object_unref(layout); 106 g_object_unref(layout);
106
107 free(buf);
108} 107}
109 108
110void pango_printf(cairo_t *cairo, const char *font, 109void pango_printf(cairo_t *cairo, const char *font,
111 double scale, bool markup, const char *fmt, ...) { 110 double scale, bool markup, const char *fmt, ...) {
112 char *buf = malloc(sizeof(char) * max_chars); 111 char buf[MAX_CHARS];
113 112
114 va_list args; 113 va_list args;
115 va_start(args, fmt); 114 va_start(args, fmt);
116 if (vsnprintf(buf, sizeof(char) * max_chars, fmt, args) >= max_chars) { 115 if (vsnprintf(buf, sizeof(buf), fmt, args) >= MAX_CHARS) {
117 strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow); 116 strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow);
118 } 117 }
119 va_end(args); 118 va_end(args);
@@ -126,6 +125,4 @@ void pango_printf(cairo_t *cairo, const char *font,
126 pango_cairo_update_layout(cairo, layout); 125 pango_cairo_update_layout(cairo, layout);
127 pango_cairo_show_layout(cairo, layout); 126 pango_cairo_show_layout(cairo, layout);
128 g_object_unref(layout); 127 g_object_unref(layout);
129
130 free(buf);
131} 128}