X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libi3%2Fipc_recv_message.c;h=84da5aa36cb90d7b913ec7c0afc87568102a9d70;hb=6a1f653508052b631afc12a16c2854cb0eefe239;hp=8fd438e3455f2bf1456e1f52fdd18d442bd0aefb;hpb=fcdfab4d280cf469f339ce7168082658b9b76c8c;p=i3%2Fi3 diff --git a/libi3/ipc_recv_message.c b/libi3/ipc_recv_message.c index 8fd438e3..84da5aa3 100644 --- a/libi3/ipc_recv_message.c +++ b/libi3/ipc_recv_message.c @@ -2,20 +2,21 @@ * vim:ts=4:sw=4:expandtab * * i3 - an improved dynamic tiling window manager - * © 2009-2013 Michael Stapelberg and contributors (see also: LICENSE) + * © 2009 Michael Stapelberg and contributors (see also: LICENSE) * */ +#include "libi3.h" + #include #include #include #include #include #include +#include #include -#include "libi3.h" - /* * Reads a message from the given socket file descriptor and stores its length * (reply_length) as well as a pointer to its contents (reply). @@ -41,34 +42,45 @@ int ipc_recv_message(int sockfd, uint32_t *message_type, if (n == -1) return -1; if (n == 0) { - ELOG("IPC: received EOF instead of reply\n"); - return -2; + if (read_bytes == 0) { + return -2; + } else { + ELOG("IPC: unexpected EOF while reading header, got %" PRIu32 " bytes, want %" PRIu32 " bytes\n", + read_bytes, to_read); + return -3; + } } read_bytes += n; } if (memcmp(walk, I3_IPC_MAGIC, strlen(I3_IPC_MAGIC)) != 0) { - ELOG("IPC: invalid magic in reply\n"); + ELOG("IPC: invalid magic in header, got \"%.*s\", want \"%s\"\n", + (int)strlen(I3_IPC_MAGIC), walk, I3_IPC_MAGIC); return -3; } walk += strlen(I3_IPC_MAGIC); - *reply_length = *((uint32_t*)walk); + memcpy(reply_length, walk, sizeof(uint32_t)); walk += sizeof(uint32_t); if (message_type != NULL) - *message_type = *((uint32_t*)walk); + memcpy(message_type, walk, sizeof(uint32_t)); *reply = smalloc(*reply_length); read_bytes = 0; - int n; while (read_bytes < *reply_length) { - if ((n = read(sockfd, *reply + read_bytes, *reply_length - read_bytes)) == -1) { + const int n = read(sockfd, *reply + read_bytes, *reply_length - read_bytes); + if (n == -1) { if (errno == EINTR || errno == EAGAIN) continue; return -1; } + if (n == 0) { + ELOG("IPC: unexpected EOF while reading payload, got %" PRIu32 " bytes, want %" PRIu32 " bytes\n", + read_bytes, *reply_length); + return -3; + } read_bytes += n; }