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 /common/ipc-client.c | |
parent | Remove usage of VLAs. (diff) | |
download | sway-de6f5b345380e80b4d59ebc569697683af064424.tar.gz sway-de6f5b345380e80b4d59ebc569697683af064424.tar.zst sway-de6f5b345380e80b4d59ebc569697683af064424.zip |
Use static arrays where possible.
Diffstat (limited to 'common/ipc-client.c')
-rw-r--r-- | common/ipc-client.c | 16 |
1 files changed, 7 insertions, 9 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 | } |