Use static arrays where possible.

This commit is contained in:
Connor E 2019-01-16 09:57:51 +00:00 committed by emersion
parent aa9d7d8ca1
commit de6f5b3453
3 changed files with 23 additions and 36 deletions

View File

@ -10,7 +10,8 @@
#include "log.h"
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
static const size_t ipc_header_size = sizeof(ipc_magic)+8;
#define IPC_HEADER_SIZE (sizeof(ipc_magic) + 8)
char *get_socketpath(void) {
const char *swaysock = getenv("SWAYSOCK");
@ -61,12 +62,12 @@ int ipc_open_socket(const char *socket_path) {
}
struct ipc_response *ipc_recv_response(int socketfd) {
char *data = malloc(sizeof(char) * ipc_header_size);
char data[IPC_HEADER_SIZE];
uint32_t *data32 = (uint32_t *)(data + sizeof(ipc_magic));
size_t total = 0;
while (total < ipc_header_size) {
ssize_t received = recv(socketfd, data + total, ipc_header_size - total, 0);
while (total < IPC_HEADER_SIZE) {
ssize_t received = recv(socketfd, data + total, IPC_HEADER_SIZE - total, 0);
if (received <= 0) {
sway_abort("Unable to receive IPC response");
}
@ -81,7 +82,6 @@ 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) {
@ -113,18 +113,16 @@ 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 = malloc(sizeof(char) * ipc_header_size);
char data[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));
memcpy(&data32[1], &type, sizeof(type));
if (write(socketfd, data, ipc_header_size) == -1) {
if (write(socketfd, data, IPC_HEADER_SIZE) == -1) {
sway_abort("Unable to send IPC header");
}
free(data);
if (write(socketfd, payload, *len) == -1) {
sway_abort("Unable to send IPC payload");
}

View File

@ -10,8 +10,9 @@
#include "log.h"
#include "stringop.h"
#define MAX_CHARS 16384
static const char overflow[] = "[buffer overflow]";
static const int max_chars = 16384;
size_t escape_markup_text(const char *src, char *dest) {
size_t length = 0;
@ -87,11 +88,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 = malloc(sizeof(char) * max_chars);
char buf[MAX_CHARS];
va_list args;
va_start(args, fmt);
if (vsnprintf(buf, sizeof(char) * max_chars, fmt, args) >= max_chars) {
if (vsnprintf(buf, sizeof(buf), fmt, args) >= MAX_CHARS) {
strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow);
}
va_end(args);
@ -103,17 +104,15 @@ 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 = malloc(sizeof(char) * max_chars);
char buf[MAX_CHARS];
va_list args;
va_start(args, fmt);
if (vsnprintf(buf, sizeof(char) * max_chars, fmt, args) >= max_chars) {
if (vsnprintf(buf, sizeof(buf), fmt, args) >= MAX_CHARS) {
strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow);
}
va_end(args);
@ -126,6 +125,4 @@ 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

@ -39,6 +39,8 @@ static struct wl_listener ipc_display_destroy;
static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
#define IPC_HEADER_SIZE (sizeof(ipc_magic) + 8)
struct ipc_client {
struct wl_event_source *event_source;
struct wl_event_source *writable_event_source;
@ -196,8 +198,6 @@ int ipc_handle_connection(int fd, uint32_t mask, void *data) {
return 0;
}
static const int ipc_header_size = sizeof(ipc_magic) + 8;
int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) {
struct ipc_client *client = data;
@ -230,33 +230,29 @@ int ipc_client_handle_readable(int client_fd, uint32_t mask, void *data) {
return 0;
}
if (read_available < ipc_header_size) {
if (read_available < (int) IPC_HEADER_SIZE) {
return 0;
}
uint8_t *buf = malloc(sizeof(uint8_t) * ipc_header_size);
uint8_t buf[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);
// 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);
}
@ -864,14 +860,14 @@ exit_cleanup:
bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length) {
assert(payload);
char *data = malloc(sizeof(char) * ipc_header_size);
char data[IPC_HEADER_SIZE];
uint32_t *data32 = (uint32_t*)(data + sizeof(ipc_magic));
memcpy(data, ipc_magic, sizeof(ipc_magic));
memcpy(&data32[0], &payload_length, sizeof(payload_length));
memcpy(&data32[1], &client->current_command, sizeof(client->current_command));
while (client->write_buffer_len + ipc_header_size + payload_length >=
while (client->write_buffer_len + IPC_HEADER_SIZE + payload_length >=
client->write_buffer_size) {
client->write_buffer_size *= 2;
}
@ -879,7 +875,6 @@ 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;
}
@ -887,18 +882,15 @@ 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;
memcpy(client->write_buffer + client->write_buffer_len, data, ipc_header_size);
client->write_buffer_len += ipc_header_size;
memcpy(client->write_buffer + client->write_buffer_len, data, IPC_HEADER_SIZE);
client->write_buffer_len += IPC_HEADER_SIZE;
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,