mirror of
https://github.com/swaywm/sway.git
synced 2024-12-26 23:18:26 +01:00
Handle malloc failure in ipc_recv_response
This commit is contained in:
parent
6c0fc20936
commit
9ad1e6b40f
3 changed files with 24 additions and 3 deletions
|
@ -52,10 +52,18 @@ struct ipc_response *ipc_recv_response(int socketfd) {
|
|||
}
|
||||
|
||||
struct ipc_response *response = malloc(sizeof(struct ipc_response));
|
||||
if (!response) {
|
||||
goto error_1;
|
||||
}
|
||||
|
||||
total = 0;
|
||||
response->size = data32[0];
|
||||
response->type = data32[1];
|
||||
char *payload = malloc(response->size + 1);
|
||||
if (!payload) {
|
||||
goto error_2;
|
||||
}
|
||||
|
||||
while (total < response->size) {
|
||||
ssize_t received = recv(socketfd, payload + total, response->size - total, 0);
|
||||
if (received < 0) {
|
||||
|
@ -67,6 +75,11 @@ struct ipc_response *ipc_recv_response(int socketfd) {
|
|||
response->payload = payload;
|
||||
|
||||
return response;
|
||||
error_2:
|
||||
free(response);
|
||||
error_1:
|
||||
sway_log(L_ERROR, "Unable to allocate memory for IPC response");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void free_ipc_response(struct ipc_response *response) {
|
||||
|
|
11
common/log.c
11
common/log.c
|
@ -88,9 +88,14 @@ void _sway_log(const char *filename, int line, log_importance_t verbosity, const
|
|||
}
|
||||
|
||||
if (filename && line) {
|
||||
char *file = strdup(filename);
|
||||
fprintf(stderr, "[%s:%d] ", basename(file), line);
|
||||
free(file);
|
||||
const char *file = filename + strlen(filename);
|
||||
while (file != filename && *file != '/') {
|
||||
--file;
|
||||
}
|
||||
if (*file == '/') {
|
||||
++file;
|
||||
}
|
||||
fprintf(stderr, "[%s:%d] ", file, line);
|
||||
}
|
||||
|
||||
va_list args;
|
||||
|
|
|
@ -331,6 +331,9 @@ void ipc_bar_init(struct bar *bar, const char *bar_id) {
|
|||
|
||||
bool handle_ipc_event(struct bar *bar) {
|
||||
struct ipc_response *resp = ipc_recv_response(bar->ipc_event_socketfd);
|
||||
if (!resp) {
|
||||
return false;
|
||||
}
|
||||
switch (resp->type) {
|
||||
case IPC_EVENT_WORKSPACE:
|
||||
ipc_update_workspaces(bar);
|
||||
|
|
Loading…
Reference in a new issue