From e4e653ada6fc729efad3f6a0d49cf72b94c43b6c Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Sat, 8 Apr 2023 23:14:12 +0100 Subject: [PATCH] socket2: receive bytes to avoid endless loops --- src/managers/EventManager.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/managers/EventManager.cpp b/src/managers/EventManager.cpp index 81acd379..3fd63fcb 100644 --- a/src/managers/EventManager.cpp +++ b/src/managers/EventManager.cpp @@ -12,14 +12,15 @@ #include #include #include +#include #include CEventManager::CEventManager() {} int fdHandleWrite(int fd, uint32_t mask, void* data) { - if (mask & WL_EVENT_ERROR || mask & WL_EVENT_HANGUP) { - // remove, hanged up + + auto removeFD = [&](int fd) -> void { const auto ACCEPTEDFDS = (std::deque>*)data; for (auto it = ACCEPTEDFDS->begin(); it != ACCEPTEDFDS->end();) { if (it->first == fd) { @@ -29,6 +30,27 @@ int fdHandleWrite(int fd, uint32_t mask, void* data) { it++; } } + }; + + if (mask & WL_EVENT_ERROR || mask & WL_EVENT_HANGUP) { + // remove, hanged up + removeFD(fd); + return 0; + } + + int availableBytes; + if (ioctl(fd, FIONREAD, &availableBytes) == -1) { + Debug::log(ERR, "fd %d sent invalid data (1)", fd); + removeFD(fd); + return 0; + } + + char buf[availableBytes]; + const auto RECEIVED = recv(fd, buf, availableBytes, 0); + if (RECEIVED == -1) { + Debug::log(ERR, "fd %d sent invalid data (2)", fd); + removeFD(fd); + return 0; } return 0;