From 3e69928f1332a631345709c72d9161f2d2ea8fbb Mon Sep 17 00:00:00 2001 From: Tobias Stoeckmann Date: Fri, 7 May 2021 21:27:33 +0200 Subject: [PATCH] common: ipc_single_command can return NULL Especially printf's %s with NULL value is undefined behavior. --- common/ipc-client.c | 12 +++++++++--- sway/main.c | 2 +- swaybar/ipc.c | 5 ++++- swaymsg/main.c | 8 ++++++++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/common/ipc-client.c b/common/ipc-client.c index 78dec010..088347eb 100644 --- a/common/ipc-client.c +++ b/common/ipc-client.c @@ -153,9 +153,15 @@ char *ipc_single_command(int socketfd, uint32_t type, const char *payload, size_ } struct ipc_response *resp = ipc_recv_response(socketfd); - char *response = resp->payload; - *len = resp->size; - free(resp); + char *response; + if (resp == NULL) { + response = NULL; + *len = 0; + } else { + response = resp->payload; + *len = resp->size; + free(resp); + } return response; } diff --git a/sway/main.c b/sway/main.c index e367ed36..95b522c5 100644 --- a/sway/main.c +++ b/sway/main.c @@ -89,7 +89,7 @@ void run_as_ipc_client(char *command, char *socket_path) { int socketfd = ipc_open_socket(socket_path); size_t len = strlen(command); char *resp = ipc_single_command(socketfd, IPC_COMMAND, command, &len); - printf("%s\n", resp); + printf("%s\n", resp == NULL ? "(null)" : resp); free(resp); close(socketfd); } diff --git a/swaybar/ipc.c b/swaybar/ipc.c index 3800a1dd..d5cf6a31 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c @@ -344,6 +344,9 @@ bool ipc_get_workspaces(struct swaybar *bar) { size_t len = 0; char *res = ipc_single_command(bar->ipc_socketfd, IPC_GET_WORKSPACES, NULL, &len); + if (res == NULL) { + return false; + } json_object *results = json_tokener_parse(res); if (!results) { free(res); @@ -418,7 +421,7 @@ bool ipc_initialize(struct swaybar *bar) { size_t len = strlen(bar->id); char *res = ipc_single_command(bar->ipc_socketfd, IPC_GET_BAR_CONFIG, bar->id, &len); - if (!ipc_parse_config(bar->config, res)) { + if (res == NULL || !ipc_parse_config(bar->config, res)) { free(res); return false; } diff --git a/swaymsg/main.c b/swaymsg/main.c index 993e08f0..95f5695f 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -478,6 +478,14 @@ int main(int argc, char **argv) { ipc_set_recv_timeout(socketfd, timeout); size_t len = strlen(command); char *resp = ipc_single_command(socketfd, type, command, &len); + if (resp == NULL) { + if (!quiet) { + sway_log(SWAY_ERROR, "Failed to receive reply"); + } + close(socketfd); + free(socket_path); + return 1; + } // pretty print the json json_object *obj = json_tokener_parse(resp);