diff --git a/nixd/librpc/include/nixd/rpc/IO.h b/nixd/librpc/include/nixd/rpc/IO.h index 64d0e04d6..9a1f2521c 100644 --- a/nixd/librpc/include/nixd/rpc/IO.h +++ b/nixd/librpc/include/nixd/rpc/IO.h @@ -7,6 +7,6 @@ namespace nixd::rpc { /// \brief Read N bytes from FD into Buf, block until N bytes are read. -size_t readBytes(int FD, void *Buf, std::size_t N); +[[nodiscard]] size_t readBytes(int FD, void *Buf, std::size_t N); } // namespace nixd::rpc diff --git a/nixd/librpc/src/Transport.cpp b/nixd/librpc/src/Transport.cpp index 7e712fce6..8601a46b9 100644 --- a/nixd/librpc/src/Transport.cpp +++ b/nixd/librpc/src/Transport.cpp @@ -1,6 +1,7 @@ #include "nixd/rpc/Transport.h" #include "nixd/rpc/IO.h" +#include #include #include @@ -20,17 +21,24 @@ void send(int OutboundFD, std::string_view Msg) { std::vector recv(int InboundFD) { MessageSizeT Size; - readBytes(InboundFD, &Size, sizeof(Size)); + if (readBytes(InboundFD, &Size, sizeof(MessageSizeT)) < + sizeof(MessageSizeT)) { + return {}; + } // Collect a message std::vector Buf(Size); - readBytes(InboundFD, Buf.data(), Size); + if (readBytes(InboundFD, Buf.data(), Size) < Size) { + throw std::runtime_error("in-complete inbound size"); + } return Buf; } int Transport::run() { for (;;) { std::vector Buf = recv(); + if (Buf.empty()) + break; handleInbound(Buf); } return 0;