diff options
author | Connor E <38229097+c-edw@users.noreply.github.com> | 2019-01-16 09:57:51 +0000 |
---|---|---|
committer | emersion <contact@emersion.fr> | 2019-01-16 13:02:26 +0100 |
commit | de6f5b345380e80b4d59ebc569697683af064424 (patch) | |
tree | 2ba6738b30a3cdcbceb0d8307424217fca448252 | |
parent | Remove usage of VLAs. (diff) | |
download | sway-de6f5b345380e80b4d59ebc569697683af064424.tar.gz sway-de6f5b345380e80b4d59ebc569697683af064424.tar.zst sway-de6f5b345380e80b4d59ebc569697683af064424.zip |
Use static arrays where possible.
-rw-r--r-- | common/ipc-client.c | 16 | ||||
-rw-r--r-- | common/pango.c | 15 | ||||
-rw-r--r-- | sway/ipc-server.c | 28 |
3 files changed, 23 insertions, 36 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 | ||
12 | static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; | 12 | static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; |
13 | static const size_t ipc_header_size = sizeof(ipc_magic)+8; | 13 | |
14 | #define IPC_HEADER_SIZE (sizeof(ipc_magic) + 8) | ||
14 | 15 | ||
15 | char *get_socketpath(void) { | 16 | char *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 | ||
63 | struct ipc_response *ipc_recv_response(int socketfd) { | 64 | struct 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 | ||
115 | char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) { | 115 | char *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 | |||
13 | static const char overflow[] = "[buffer overflow]"; | 15 | static const char overflow[] = "[buffer overflow]"; |
14 | static const int max_chars = 16384; | ||
15 | 16 | ||
16 | size_t escape_markup_text(const char *src, char *dest) { | 17 | size_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 | ||
88 | void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, | 89 | void 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 | ||
110 | void pango_printf(cairo_t *cairo, const char *font, | 109 | void 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 | } |
diff --git a/sway/ipc-server.c b/sway/ipc-server.c index 73fa8ae5..9a033a4b 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c | |||
@@ -39,6 +39,8 @@ static struct wl_listener ipc_display_destroy; | |||
39 | 39 | ||
40 | static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; | 40 | static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'}; |
41 | 41 | ||
42 | #define IPC_HEADER_SIZE (sizeof(ipc_magic) + 8) | ||
43 | |||
42 | struct ipc_client { | 44 | struct ipc_client { |
43 | struct wl_event_source *event_source; | 45 | struct wl_event_source *event_source; |
44 | struct wl_event_source *writable_event_source; | 46 | struct wl_event_source *writable_event_source; |
@@ -196,8 +198,6 @@ int ipc_handle_connection(int fd, uint32_t mask, void *data) { | |||
196 | return 0; | 198 | return 0; |
197 | } | 199 | } |
198 | 200 | ||
199 | static const int ipc_header_size = sizeof(ipc_magic) + 8; | ||
200 | |||
201 | int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) { | 201 | int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) { |
202 | struct ipc_client *client = data; | 202 | struct ipc_client *client = data; |
203 | 203 | ||
@@ -230,33 +230,29 @@ int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) { | |||
230 | return 0; | 230 | return 0; |
231 | } | 231 | } |
232 | 232 | ||
233 | if (read_available < ipc_header_size) { | 233 | if (read_available < (int) IPC_HEADER_SIZE) { |
234 | return 0; | 234 | return 0; |
235 | } | 235 | } |
236 | 236 | ||
237 | uint8_t *buf = malloc(sizeof(uint8_t) * ipc_header_size); | 237 | uint8_t buf[IPC_HEADER_SIZE]; |
238 | uint32_t *buf32 = (uint32_t*)(buf + sizeof(ipc_magic)); | 238 | uint32_t *buf32 = (uint32_t*)(buf + sizeof(ipc_magic)); |
239 | // Should be fully available, because read_available >= ipc_header_size | 239 | // Should be fully available, because read_available >= IPC_HEADER_SIZE |
240 | ssize_t received = recv(client_fd, buf, ipc_header_size, 0); | 240 | ssize_t received = recv(client_fd, buf, IPC_HEADER_SIZE, 0); |
241 | if (received == -1) { | 241 | if (received == -1) { |
242 | wlr_log_errno(WLR_INFO, "Unable to receive header from IPC client"); | 242 | wlr_log_errno(WLR_INFO, "Unable to receive header from IPC client"); |
243 | ipc_client_disconnect(client); | 243 | ipc_client_disconnect(client); |
244 | free(buf); | ||
245 | return 0; | 244 | return 0; |
246 | } | 245 | } |
247 | 246 | ||
248 | if (memcmp(buf, ipc_magic, sizeof(ipc_magic)) != 0) { | 247 | if (memcmp(buf, ipc_magic, sizeof(ipc_magic)) != 0) { |
249 | wlr_log(WLR_DEBUG, "IPC header check failed"); | 248 | wlr_log(WLR_DEBUG, "IPC header check failed"); |
250 | ipc_client_disconnect(client); | 249 | ipc_client_disconnect(client); |
251 | free(buf); | ||
252 | return 0; | 250 | return 0; |
253 | } | 251 | } |
254 | 252 | ||
255 | memcpy(&client->payload_length, &buf32[0], sizeof(buf32[0])); | 253 | memcpy(&client->payload_length, &buf32[0], sizeof(buf32[0])); |
256 | memcpy(&client->current_command, &buf32[1], sizeof(buf32[1])); | 254 | memcpy(&client->current_command, &buf32[1], sizeof(buf32[1])); |
257 | 255 | ||
258 | free(buf); | ||
259 | |||
260 | if (read_available - received >= (long)client->payload_length) { | 256 | if (read_available - received >= (long)client->payload_length) { |
261 | ipc_client_handle_command(client); | 257 | ipc_client_handle_command(client); |
262 | } | 258 | } |
@@ -864,14 +860,14 @@ exit_cleanup: | |||
864 | bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length) { | 860 | bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length) { |
865 | assert(payload); | 861 | assert(payload); |
866 | 862 | ||
867 | char *data = malloc(sizeof(char) * ipc_header_size); | 863 | char data[IPC_HEADER_SIZE]; |
868 | uint32_t *data32 = (uint32_t*)(data + sizeof(ipc_magic)); | 864 | uint32_t *data32 = (uint32_t*)(data + sizeof(ipc_magic)); |
869 | 865 | ||
870 | memcpy(data, ipc_magic, sizeof(ipc_magic)); | 866 | memcpy(data, ipc_magic, sizeof(ipc_magic)); |
871 | memcpy(&data32[0], &payload_length, sizeof(payload_length)); | 867 | memcpy(&data32[0], &payload_length, sizeof(payload_length)); |
872 | memcpy(&data32[1], &client->current_command, sizeof(client->current_command)); | 868 | memcpy(&data32[1], &client->current_command, sizeof(client->current_command)); |
873 | 869 | ||
874 | while (client->write_buffer_len + ipc_header_size + payload_length >= | 870 | while (client->write_buffer_len + IPC_HEADER_SIZE + payload_length >= |
875 | client->write_buffer_size) { | 871 | client->write_buffer_size) { |
876 | client->write_buffer_size *= 2; | 872 | client->write_buffer_size *= 2; |
877 | } | 873 | } |
@@ -879,7 +875,6 @@ bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t pay | |||
879 | if (client->write_buffer_size > 4e6) { // 4 MB | 875 | if (client->write_buffer_size > 4e6) { // 4 MB |
880 | wlr_log(WLR_ERROR, "Client write buffer too big, disconnecting client"); | 876 | wlr_log(WLR_ERROR, "Client write buffer too big, disconnecting client"); |
881 | ipc_client_disconnect(client); | 877 | ipc_client_disconnect(client); |
882 | free(data); | ||
883 | return false; | 878 | return false; |
884 | } | 879 | } |
885 | 880 | ||
@@ -887,18 +882,15 @@ bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t pay | |||
887 | if (!new_buffer) { | 882 | if (!new_buffer) { |
888 | wlr_log(WLR_ERROR, "Unable to reallocate ipc client write buffer"); | 883 | wlr_log(WLR_ERROR, "Unable to reallocate ipc client write buffer"); |
889 | ipc_client_disconnect(client); | 884 | ipc_client_disconnect(client); |
890 | free(data); | ||
891 | return false; | 885 | return false; |
892 | } | 886 | } |
893 | client->write_buffer = new_buffer; | 887 | client->write_buffer = new_buffer; |
894 | 888 | ||
895 | memcpy(client->write_buffer + client->write_buffer_len, data, ipc_header_size); | 889 | memcpy(client->write_buffer + client->write_buffer_len, data, IPC_HEADER_SIZE); |
896 | client->write_buffer_len += ipc_header_size; | 890 | client->write_buffer_len += IPC_HEADER_SIZE; |
897 | memcpy(client->write_buffer + client->write_buffer_len, payload, payload_length); | 891 | memcpy(client->write_buffer + client->write_buffer_len, payload, payload_length); |
898 | client->write_buffer_len += payload_length; | 892 | client->write_buffer_len += payload_length; |
899 | 893 | ||
900 | free(data); | ||
901 | |||
902 | if (!client->writable_event_source) { | 894 | if (!client->writable_event_source) { |
903 | client->writable_event_source = wl_event_loop_add_fd( | 895 | client->writable_event_source = wl_event_loop_add_fd( |
904 | server.wl_event_loop, client->fd, WL_EVENT_WRITABLE, | 896 | server.wl_event_loop, client->fd, WL_EVENT_WRITABLE, |