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) {
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");
}

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,
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);
}

View File

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

View File

@ -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,

View File

@ -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) {

View File

@ -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;
}

View File

@ -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;
}