common: ipc_single_command can return NULL

Especially printf's %s with NULL value is undefined behavior.
This commit is contained in:
Tobias Stoeckmann 2021-05-07 21:27:33 +02:00
parent 06ab0d166a
commit 3e69928f13
4 changed files with 22 additions and 5 deletions

View file

@ -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); struct ipc_response *resp = ipc_recv_response(socketfd);
char *response = resp->payload; char *response;
*len = resp->size; if (resp == NULL) {
free(resp); response = NULL;
*len = 0;
} else {
response = resp->payload;
*len = resp->size;
free(resp);
}
return response; return response;
} }

View file

@ -89,7 +89,7 @@ void run_as_ipc_client(char *command, char *socket_path) {
int socketfd = ipc_open_socket(socket_path); int socketfd = ipc_open_socket(socket_path);
size_t len = strlen(command); size_t len = strlen(command);
char *resp = ipc_single_command(socketfd, IPC_COMMAND, command, &len); char *resp = ipc_single_command(socketfd, IPC_COMMAND, command, &len);
printf("%s\n", resp); printf("%s\n", resp == NULL ? "(null)" : resp);
free(resp); free(resp);
close(socketfd); close(socketfd);
} }

View file

@ -344,6 +344,9 @@ bool ipc_get_workspaces(struct swaybar *bar) {
size_t len = 0; size_t len = 0;
char *res = ipc_single_command(bar->ipc_socketfd, char *res = ipc_single_command(bar->ipc_socketfd,
IPC_GET_WORKSPACES, NULL, &len); IPC_GET_WORKSPACES, NULL, &len);
if (res == NULL) {
return false;
}
json_object *results = json_tokener_parse(res); json_object *results = json_tokener_parse(res);
if (!results) { if (!results) {
free(res); free(res);
@ -418,7 +421,7 @@ bool ipc_initialize(struct swaybar *bar) {
size_t len = strlen(bar->id); size_t len = strlen(bar->id);
char *res = ipc_single_command(bar->ipc_socketfd, char *res = ipc_single_command(bar->ipc_socketfd,
IPC_GET_BAR_CONFIG, bar->id, &len); 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); free(res);
return false; return false;
} }

View file

@ -478,6 +478,14 @@ int main(int argc, char **argv) {
ipc_set_recv_timeout(socketfd, timeout); ipc_set_recv_timeout(socketfd, timeout);
size_t len = strlen(command); size_t len = strlen(command);
char *resp = ipc_single_command(socketfd, type, command, &len); 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 // pretty print the json
json_object *obj = json_tokener_parse(resp); json_object *obj = json_tokener_parse(resp);