From aa9d7d8ca19f4489839f765ad7f190e8141bd001 Mon Sep 17 00:00:00 2001 From: Connor E <38229097+c-edw@users.noreply.github.com> Date: Wed, 16 Jan 2019 01:57:53 +0000 Subject: [PATCH] Remove usage of VLAs. --- common/ipc-client.c | 9 +++++++-- common/pango.c | 12 ++++++++---- meson.build | 1 + sway/ipc-server.c | 12 ++++++++++-- swaybar/ipc.c | 3 ++- swaynag/config.c | 3 ++- swaynag/swaynag.c | 3 ++- 7 files changed, 32 insertions(+), 11 deletions(-) diff --git a/common/ipc-client.c b/common/ipc-client.c index 3515ef0a..13fd8a05 100644 --- a/common/ipc-client.c +++ b/common/ipc-client.c @@ -61,7 +61,7 @@ int ipc_open_socket(const char *socket_path) { } struct ipc_response *ipc_recv_response(int socketfd) { - char data[ipc_header_size]; + char *data = malloc(sizeof(char) * ipc_header_size); uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); size_t total = 0; @@ -81,6 +81,8 @@ struct ipc_response *ipc_recv_response(int socketfd) { total = 0; memcpy(&response->size, &data32[0], sizeof(data32[0])); memcpy(&response->type, &data32[1], sizeof(data32[1])); + free(data); + char *payload = malloc(response->size + 1); if (!payload) { goto error_2; @@ -99,6 +101,7 @@ struct ipc_response *ipc_recv_response(int socketfd) { return response; error_2: free(response); + free(payload); error_1: wlr_log(WLR_ERROR, "Unable to allocate memory for IPC response"); return NULL; @@ -110,7 +113,7 @@ void free_ipc_response(struct ipc_response *response) { } char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint32_t *len) { - char data[ipc_header_size]; + char *data = malloc(sizeof(char) * ipc_header_size); uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic)); memcpy(data, ipc_magic, sizeof(ipc_magic)); memcpy(&data32[0], len, sizeof(*len)); @@ -120,6 +123,8 @@ char *ipc_single_command(int socketfd, uint32_t type, const char *payload, uint3 sway_abort("Unable to send IPC header"); } + free(data); + if (write(socketfd, payload, *len) == -1) { sway_abort("Unable to send IPC payload"); } diff --git a/common/pango.c b/common/pango.c index 3bc97808..18b92e9d 100644 --- a/common/pango.c +++ b/common/pango.c @@ -87,11 +87,11 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, int *baseline, double scale, bool markup, const char *fmt, ...) { - char buf[max_chars]; + char *buf = malloc(sizeof(char) * max_chars); va_list args; va_start(args, fmt); - if (vsnprintf(buf, sizeof(buf), fmt, args) >= max_chars) { + if (vsnprintf(buf, sizeof(char) * max_chars, fmt, args) >= max_chars) { strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow); } va_end(args); @@ -103,15 +103,17 @@ void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, *baseline = pango_layout_get_baseline(layout) / PANGO_SCALE; } g_object_unref(layout); + + free(buf); } void pango_printf(cairo_t *cairo, const char *font, double scale, bool markup, const char *fmt, ...) { - char buf[max_chars]; + char *buf = malloc(sizeof(char) * max_chars); va_list args; va_start(args, fmt); - if (vsnprintf(buf, sizeof(buf), fmt, args) >= max_chars) { + if (vsnprintf(buf, sizeof(char) * max_chars, fmt, args) >= max_chars) { strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow); } va_end(args); @@ -124,4 +126,6 @@ void pango_printf(cairo_t *cairo, const char *font, pango_cairo_update_layout(cairo, layout); pango_cairo_show_layout(cairo, layout); g_object_unref(layout); + + free(buf); } diff --git a/meson.build b/meson.build index 520a87ad..2285cbdf 100644 --- a/meson.build +++ b/meson.build @@ -17,6 +17,7 @@ add_project_arguments( '-Wno-unused-parameter', '-Wno-unused-result', '-Wundef', + '-Wvla', ], language: 'c', ) diff --git a/sway/ipc-server.c b/sway/ipc-server.c index ff1bc89f..73fa8ae5 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -234,25 +234,29 @@ int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) { return 0; } - uint8_t buf[ipc_header_size]; + uint8_t *buf = malloc(sizeof(uint8_t) * ipc_header_size); uint32_t *buf32 = (uint32_t*)(buf + sizeof(ipc_magic)); // Should be fully available, because read_available >= ipc_header_size ssize_t received = recv(client_fd, buf, ipc_header_size, 0); if (received == -1) { wlr_log_errno(WLR_INFO, "Unable to receive header from IPC client"); ipc_client_disconnect(client); + free(buf); return 0; } if (memcmp(buf, ipc_magic, sizeof(ipc_magic)) != 0) { wlr_log(WLR_DEBUG, "IPC header check failed"); ipc_client_disconnect(client); + free(buf); return 0; } memcpy(&client->payload_length, &buf32[0], sizeof(buf32[0])); memcpy(&client->current_command, &buf32[1], sizeof(buf32[1])); + free(buf); + if (read_available - received >= (long)client->payload_length) { ipc_client_handle_command(client); } @@ -860,7 +864,7 @@ exit_cleanup: bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length) { assert(payload); - char data[ipc_header_size]; + char *data = malloc(sizeof(char) * ipc_header_size); uint32_t *data32 = (uint32_t*)(data + sizeof(ipc_magic)); memcpy(data, ipc_magic, sizeof(ipc_magic)); @@ -875,6 +879,7 @@ bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t pay if (client->write_buffer_size > 4e6) { // 4 MB wlr_log(WLR_ERROR, "Client write buffer too big, disconnecting client"); ipc_client_disconnect(client); + free(data); return false; } @@ -882,6 +887,7 @@ bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t pay if (!new_buffer) { wlr_log(WLR_ERROR, "Unable to reallocate ipc client write buffer"); ipc_client_disconnect(client); + free(data); return false; } client->write_buffer = new_buffer; @@ -891,6 +897,8 @@ bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t pay memcpy(client->write_buffer + client->write_buffer_len, payload, payload_length); client->write_buffer_len += payload_length; + free(data); + if (!client->writable_event_source) { client->writable_event_source = wl_event_loop_add_fd( server.wl_event_loop, client->fd, WL_EVENT_WRITABLE, diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 0dc39439..9fe3e626 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -13,9 +13,10 @@ void ipc_send_workspace_command(struct swaybar *bar, const char *ws) { const char *fmt = "workspace \"%s\""; uint32_t size = snprintf(NULL, 0, fmt, ws); - char command[size]; + char *command = malloc(sizeof(char) * size); snprintf(command, size, fmt, ws); ipc_single_command(bar->ipc_socketfd, IPC_COMMAND, command, &size); + free(command); } char *parse_font(const char *font) { diff --git a/swaynag/config.c b/swaynag/config.c index 60bda3e0..b4c22443 100644 --- a/swaynag/config.c +++ b/swaynag/config.c @@ -365,11 +365,12 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { } free(name); } else { - char flag[nread + 3]; + char *flag = malloc(sizeof(char) * (nread + 3)); sprintf(flag, "--%s", line); char *argv[] = {"swaynag", flag}; result = swaynag_parse_options(2, argv, swaynag, types, type, NULL, NULL); + free(flag); if (result != 0) { break; } diff --git a/swaynag/swaynag.c b/swaynag/swaynag.c index 674c24b5..ceb795a8 100644 --- a/swaynag/swaynag.c +++ b/swaynag/swaynag.c @@ -28,9 +28,10 @@ static bool terminal_execute(char *terminal, char *command) { fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command); fclose(tmp); chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR); - char cmd[strlen(terminal) + strlen(" -e ") + strlen(fname) + 1]; + char *cmd = malloc(sizeof(char) * (strlen(terminal) + strlen(" -e ") + strlen(fname) + 1)); sprintf(cmd, "%s -e %s", terminal, fname); execl("/bin/sh", "/bin/sh", "-c", cmd, NULL); + free(cmd); return true; }