From 340ab17bf16f265e64c9ed282abd32b09aa42de2 Mon Sep 17 00:00:00 2001 From: Rapha Date: Sun, 20 Sep 2026 23:07:01 +0200 Subject: [PATCH 1/3] fix: maxMsgLength should be 510, independant from line ending (\r\n or just \n) --- srcs/server/Socket.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/srcs/server/Socket.cpp b/srcs/server/Socket.cpp index a574bfa..2dac0ba 100644 --- a/srcs/server/Socket.cpp +++ b/srcs/server/Socket.cpp @@ -91,10 +91,15 @@ size_t Socket::bufferLengthAndSanityCheck_(SocketIOData &sIOD, std::string &rdBuffer = sIOD.readBuffer; size_t eol = rdBuffer.find('\n'); + size_t eolMaxIdxPos = MAXMSGLEN - 2; + if ((eol > 0) && (eol != std::string::npos) && + (rdBuffer.at(eol - 1) == '\r')) + eolMaxIdxPos = MAXMSGLEN - 1; + msgTooLong = false; if (!sIOD.discardMode) { if ((rdBuffer.length() < MAXMSGLEN) || - (eol != std::string::npos && eol < MAXMSGLEN)) { + (eol != std::string::npos && (eol <= eolMaxIdxPos))) { return eol; } else { sIOD.discardMode = true; From 1a4d08657170f6260e41763f3d6e7fbf707c76f2 Mon Sep 17 00:00:00 2001 From: Rapha Date: Sun, 20 Sep 2026 23:28:23 +0200 Subject: [PATCH 2/3] fix: returned position must always be the actual end-of-line position --- srcs/server/Socket.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/srcs/server/Socket.cpp b/srcs/server/Socket.cpp index 2dac0ba..fbd7afa 100644 --- a/srcs/server/Socket.cpp +++ b/srcs/server/Socket.cpp @@ -76,8 +76,6 @@ std::string Socket::getCRLFLineFromBuffer(int const fd, bool &buffHasMoreCmds, std::string cmdLine; if (eol != std::string::npos) { cmdLine = sIOD.readBuffer.substr(0, eol); - if (!cmdLine.empty() && *cmdLine.rbegin() == '\r') - cmdLine.erase(cmdLine.size() - 1); sIOD.recvErrorCount = 0; sIOD.readBuffer = sIOD.readBuffer.substr(eol + 1); } @@ -89,18 +87,19 @@ std::string Socket::getCRLFLineFromBuffer(int const fd, bool &buffHasMoreCmds, size_t Socket::bufferLengthAndSanityCheck_(SocketIOData &sIOD, bool &msgTooLong) { std::string &rdBuffer = sIOD.readBuffer; - size_t eol = rdBuffer.find('\n'); + size_t const eolMaxPos = MAXMSGLEN - 2; + size_t eolPos = rdBuffer.find('\n'); - size_t eolMaxIdxPos = MAXMSGLEN - 2; - if ((eol > 0) && (eol != std::string::npos) && - (rdBuffer.at(eol - 1) == '\r')) - eolMaxIdxPos = MAXMSGLEN - 1; + if ((eolPos > 0) && (eolPos != std::string::npos) && + (rdBuffer.at(eolPos - 1) == '\r')) { + eolPos -= 1; + } msgTooLong = false; if (!sIOD.discardMode) { if ((rdBuffer.length() < MAXMSGLEN) || - (eol != std::string::npos && (eol <= eolMaxIdxPos))) { - return eol; + ((eolPos != std::string::npos) && (eolPos <= eolMaxPos))) { + return eolPos; } else { sIOD.discardMode = true; msgTooLong = true; @@ -111,8 +110,8 @@ size_t Socket::bufferLengthAndSanityCheck_(SocketIOData &sIOD, sIOD.closeFd = true; return std::string::npos; } - if (eol != std::string::npos) { - rdBuffer.erase(0, eol + 1); + if (eolPos != std::string::npos) { + rdBuffer.erase(0, eolPos + 1); sIOD.discardMode = false; return std::string::npos; } From 5836acb6152da784f6e5d3ca831006f67858217b Mon Sep 17 00:00:00 2001 From: Rapha Date: Mon, 21 Sep 2026 11:59:24 +0200 Subject: [PATCH 3/3] fix: erase buffer content from correct position, depending on one or two char eol --- includes/Socket.hpp | 3 ++- srcs/server/Socket.cpp | 23 +++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/includes/Socket.hpp b/includes/Socket.hpp index 380cb02..e208f71 100644 --- a/includes/Socket.hpp +++ b/includes/Socket.hpp @@ -32,7 +32,8 @@ class Socket { std::map sockIODataStore_; Logger const &logger_; - size_t bufferLengthAndSanityCheck_(SocketIOData &sIOD, bool &msgTooLong); + size_t bufferLengthAndSanityCheck_(SocketIOData &sIOD, bool &msgTooLong, + size_t &eolSize); bool checkSocket4Recv(struct pollfd &pfd, SocketIOData &sIOD); // NOLINT public: diff --git a/srcs/server/Socket.cpp b/srcs/server/Socket.cpp index fbd7afa..d7dd603 100644 --- a/srcs/server/Socket.cpp +++ b/srcs/server/Socket.cpp @@ -72,27 +72,30 @@ std::string Socket::getCRLFLineFromBuffer(int const fd, bool &buffHasMoreCmds, return ""; SocketIOData &sIOD = *sIODptr; - size_t eol = bufferLengthAndSanityCheck_(sIOD, msgTooLong); + size_t eolSize = 0; + size_t eol = bufferLengthAndSanityCheck_(sIOD, msgTooLong, eolSize); std::string cmdLine; if (eol != std::string::npos) { cmdLine = sIOD.readBuffer.substr(0, eol); sIOD.recvErrorCount = 0; - sIOD.readBuffer = sIOD.readBuffer.substr(eol + 1); + sIOD.readBuffer = sIOD.readBuffer.substr(eol + eolSize); } buffHasMoreCmds = ((sIOD.readBuffer.size() > MAXMSGLEN) || (sIOD.readBuffer.find('\n') != std::string::npos)); return cmdLine; } -size_t Socket::bufferLengthAndSanityCheck_(SocketIOData &sIOD, - bool &msgTooLong) { +size_t Socket::bufferLengthAndSanityCheck_(SocketIOData &sIOD, bool &msgTooLong, + size_t &eolSize) { std::string &rdBuffer = sIOD.readBuffer; size_t const eolMaxPos = MAXMSGLEN - 2; size_t eolPos = rdBuffer.find('\n'); + eolSize = 1; if ((eolPos > 0) && (eolPos != std::string::npos) && (rdBuffer.at(eolPos - 1) == '\r')) { eolPos -= 1; + eolSize += 1; } msgTooLong = false; @@ -106,16 +109,12 @@ size_t Socket::bufferLengthAndSanityCheck_(SocketIOData &sIOD, } } // discard mode - if (++sIOD.recvErrorCount >= SOCKETERRORLIMIT) { + if (++sIOD.recvErrorCount >= SOCKETERRORLIMIT) sIOD.closeFd = true; - return std::string::npos; - } - if (eolPos != std::string::npos) { - rdBuffer.erase(0, eolPos + 1); + else if (eolPos != std::string::npos) { + rdBuffer.erase(0, eolPos + eolSize); sIOD.discardMode = false; - return std::string::npos; - } - if (!rdBuffer.empty() && '\r' == *rdBuffer.rbegin()) + } else if (!rdBuffer.empty() && '\r' == *rdBuffer.rbegin()) rdBuffer = "\r"; else rdBuffer.clear();