Remove usage of VLAs.

This commit is contained in:
Connor E 2019-01-16 01:57:53 +00:00 committed by emersion
parent 02bbefda20
commit aa9d7d8ca1
7 changed files with 32 additions and 11 deletions

View file

@ -61,7 +61,7 @@ int ipc_open_socket(const char *socket_path) {
} }
struct ipc_response *ipc_recv_response(int socketfd) { 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)); uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
size_t total = 0; size_t total = 0;
@ -81,6 +81,8 @@ struct ipc_response *ipc_recv_response(int socketfd) {
total = 0; total = 0;
memcpy(&response->size, &data32[0], sizeof(data32[0])); memcpy(&response->size, &data32[0], sizeof(data32[0]));
memcpy(&response->type, &data32[1], sizeof(data32[1])); memcpy(&response->type, &data32[1], sizeof(data32[1]));
free(data);
char *payload = malloc(response->size + 1); char *payload = malloc(response->size + 1);
if (!payload) { if (!payload) {
goto error_2; goto error_2;
@ -99,6 +101,7 @@ struct ipc_response *ipc_recv_response(int socketfd) {
return response; return response;
error_2: error_2:
free(response); free(response);
free(payload);
error_1: error_1:
wlr_log(WLR_ERROR, "Unable to allocate memory for IPC response"); wlr_log(WLR_ERROR, "Unable to allocate memory for IPC response");
return NULL; 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 *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)); uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
memcpy(data, ipc_magic, sizeof(ipc_magic)); memcpy(data, ipc_magic, sizeof(ipc_magic));
memcpy(&data32[0], len, sizeof(*len)); 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"); sway_abort("Unable to send IPC header");
} }
free(data);
if (write(socketfd, payload, *len) == -1) { if (write(socketfd, payload, *len) == -1) {
sway_abort("Unable to send IPC payload"); sway_abort("Unable to send IPC payload");
} }

View file

@ -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, void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
int *baseline, double scale, bool markup, const char *fmt, ...) { int *baseline, double scale, bool markup, const char *fmt, ...) {
char buf[max_chars]; char *buf = malloc(sizeof(char) * max_chars);
va_list args; va_list args;
va_start(args, fmt); 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); strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow);
} }
va_end(args); 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; *baseline = pango_layout_get_baseline(layout) / PANGO_SCALE;
} }
g_object_unref(layout); g_object_unref(layout);
free(buf);
} }
void pango_printf(cairo_t *cairo, const char *font, void pango_printf(cairo_t *cairo, const char *font,
double scale, bool markup, const char *fmt, ...) { double scale, bool markup, const char *fmt, ...) {
char buf[max_chars]; char *buf = malloc(sizeof(char) * max_chars);
va_list args; va_list args;
va_start(args, fmt); 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); strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow);
} }
va_end(args); va_end(args);
@ -124,4 +126,6 @@ void pango_printf(cairo_t *cairo, const char *font,
pango_cairo_update_layout(cairo, layout); pango_cairo_update_layout(cairo, layout);
pango_cairo_show_layout(cairo, layout); pango_cairo_show_layout(cairo, layout);
g_object_unref(layout); g_object_unref(layout);
free(buf);
} }

View file

@ -17,6 +17,7 @@ add_project_arguments(
'-Wno-unused-parameter', '-Wno-unused-parameter',
'-Wno-unused-result', '-Wno-unused-result',
'-Wundef', '-Wundef',
'-Wvla',
], ],
language: 'c', language: 'c',
) )

View file

@ -234,25 +234,29 @@ int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) {
return 0; 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)); uint32_t *buf32 = (uint32_t*)(buf + sizeof(ipc_magic));
// Should be fully available, because read_available >= ipc_header_size // Should be fully available, because read_available >= ipc_header_size
ssize_t received = recv(client_fd, buf, ipc_header_size, 0); ssize_t received = recv(client_fd, buf, ipc_header_size, 0);
if (received == -1) { if (received == -1) {
wlr_log_errno(WLR_INFO, "Unable to receive header from IPC client"); wlr_log_errno(WLR_INFO, "Unable to receive header from IPC client");
ipc_client_disconnect(client); ipc_client_disconnect(client);
free(buf);
return 0; return 0;
} }
if (memcmp(buf, ipc_magic, sizeof(ipc_magic)) != 0) { if (memcmp(buf, ipc_magic, sizeof(ipc_magic)) != 0) {
wlr_log(WLR_DEBUG, "IPC header check failed"); wlr_log(WLR_DEBUG, "IPC header check failed");
ipc_client_disconnect(client); ipc_client_disconnect(client);
free(buf);
return 0; return 0;
} }
memcpy(&client->payload_length, &buf32[0], sizeof(buf32[0])); memcpy(&client->payload_length, &buf32[0], sizeof(buf32[0]));
memcpy(&client->current_command, &buf32[1], sizeof(buf32[1])); memcpy(&client->current_command, &buf32[1], sizeof(buf32[1]));
free(buf);
if (read_available - received >= (long)client->payload_length) { if (read_available - received >= (long)client->payload_length) {
ipc_client_handle_command(client); 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) { bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length) {
assert(payload); assert(payload);
char data[ipc_header_size]; char *data = malloc(sizeof(char) * ipc_header_size);
uint32_t *data32 = (uint32_t*)(data + sizeof(ipc_magic)); uint32_t *data32 = (uint32_t*)(data + sizeof(ipc_magic));
memcpy(data, ipc_magic, 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 if (client->write_buffer_size > 4e6) { // 4 MB
wlr_log(WLR_ERROR, "Client write buffer too big, disconnecting client"); wlr_log(WLR_ERROR, "Client write buffer too big, disconnecting client");
ipc_client_disconnect(client); ipc_client_disconnect(client);
free(data);
return false; return false;
} }
@ -882,6 +887,7 @@ bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t pay
if (!new_buffer) { if (!new_buffer) {
wlr_log(WLR_ERROR, "Unable to reallocate ipc client write buffer"); wlr_log(WLR_ERROR, "Unable to reallocate ipc client write buffer");
ipc_client_disconnect(client); ipc_client_disconnect(client);
free(data);
return false; return false;
} }
client->write_buffer = new_buffer; 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); memcpy(client->write_buffer + client->write_buffer_len, payload, payload_length);
client->write_buffer_len += payload_length; client->write_buffer_len += payload_length;
free(data);
if (!client->writable_event_source) { if (!client->writable_event_source) {
client->writable_event_source = wl_event_loop_add_fd( client->writable_event_source = wl_event_loop_add_fd(
server.wl_event_loop, client->fd, WL_EVENT_WRITABLE, server.wl_event_loop, client->fd, WL_EVENT_WRITABLE,

View file

@ -13,9 +13,10 @@
void ipc_send_workspace_command(struct swaybar *bar, const char *ws) { void ipc_send_workspace_command(struct swaybar *bar, const char *ws) {
const char *fmt = "workspace \"%s\""; const char *fmt = "workspace \"%s\"";
uint32_t size = snprintf(NULL, 0, fmt, ws); uint32_t size = snprintf(NULL, 0, fmt, ws);
char command[size]; char *command = malloc(sizeof(char) * size);
snprintf(command, size, fmt, ws); snprintf(command, size, fmt, ws);
ipc_single_command(bar->ipc_socketfd, IPC_COMMAND, command, &size); ipc_single_command(bar->ipc_socketfd, IPC_COMMAND, command, &size);
free(command);
} }
char *parse_font(const char *font) { char *parse_font(const char *font) {

View file

@ -365,11 +365,12 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) {
} }
free(name); free(name);
} else { } else {
char flag[nread + 3]; char *flag = malloc(sizeof(char) * (nread + 3));
sprintf(flag, "--%s", line); sprintf(flag, "--%s", line);
char *argv[] = {"swaynag", flag}; char *argv[] = {"swaynag", flag};
result = swaynag_parse_options(2, argv, swaynag, types, type, result = swaynag_parse_options(2, argv, swaynag, types, type,
NULL, NULL); NULL, NULL);
free(flag);
if (result != 0) { if (result != 0) {
break; break;
} }

View file

@ -28,9 +28,10 @@ static bool terminal_execute(char *terminal, char *command) {
fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command); fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command);
fclose(tmp); fclose(tmp);
chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR); 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); sprintf(cmd, "%s -e %s", terminal, fname);
execl("/bin/sh", "/bin/sh", "-c", cmd, NULL); execl("/bin/sh", "/bin/sh", "-c", cmd, NULL);
free(cmd);
return true; return true;
} }