From 92986bf0f996ac112d236b60123cab58d688e7ac Mon Sep 17 00:00:00 2001 From: Samy Ben Tayeb Date: Mon, 23 Jun 2025 15:29:48 +0200 Subject: [PATCH] leaks and invalid read size fixed, commande kick with argument comment fixed --- include/user.hpp | 3 +-- sources/channel/channel.cpp | 7 +------ sources/commands/invite.cpp | 10 +++++----- sources/commands/kick.cpp | 13 ++++++++----- sources/core/Server.cpp | 11 ++++++++--- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/include/user.hpp b/include/user.hpp index c01516d..952da89 100644 --- a/include/user.hpp +++ b/include/user.hpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/20 21:57:49 by rparodi #+# #+# */ -/* Updated: 2025/06/21 14:38:06 by sben-tay ### ########.fr */ +/* Updated: 2025/06/23 14:55:06 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,7 +39,6 @@ class User public: User(short unsigned fd, PollManager& poll); - User( void ); // default constructor for bot service short unsigned int getFd() const; void appendToReadBuffer(const std::string &data); void appendToWriteBuffer(const std::string &data); diff --git a/sources/channel/channel.cpp b/sources/channel/channel.cpp index 81872e4..ec26b0d 100644 --- a/sources/channel/channel.cpp +++ b/sources/channel/channel.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/20 22:43:24 by rparodi #+# #+# */ -/* Updated: 2025/06/21 19:04:34 by sben-tay ### ########.fr */ +/* Updated: 2025/06/23 14:55:34 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,11 +25,6 @@ Channel::Channel(const std::string &name, User *owner, size_t maxUsers, bool nee } Channel::~Channel() { - for (std::list::iterator it = _users.begin(); it != _users.end(); ++it) { - if (*it != _owner) { - delete *it; - } - } _users.clear(); _operators.clear(); _invited.clear(); diff --git a/sources/commands/invite.cpp b/sources/commands/invite.cpp index 7741638..0754f37 100644 --- a/sources/commands/invite.cpp +++ b/sources/commands/invite.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ -/* Updated: 2025/06/20 16:57:53 by rparodi ### ########.fr */ +/* Updated: 2025/06/23 15:01:35 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,13 +22,13 @@ e_code Invite::checkArgs() { this->_sender->appendToWriteBuffer(msg461); return ERR_NEEDMOREPARAMS; } - if (_args.at(1).at(0) != '#') { + if (_args.at(2).at(0) != '#') { WARNING_MSG("Invalid channel name for INVITE command"); INFO_MSG("Channel names must start with a '#' character"); return ERR_NOSUCHCHANNEL; } else - _args.at(1).erase(0, 1); - _cTarget = searchList(_server->getChannelsList(), _args.at(1)); + _args.at(2).erase(0, 1); + _cTarget = searchList(_server->getChannelsList(), _args.at(2)); if (_cTarget == NULL) { WARNING_MSG("Channel not found for INVITE command"); INFO_MSG("You can only invite users to channels you are in"); @@ -38,7 +38,7 @@ e_code Invite::checkArgs() { WARNING_MSG("You are not an operator in the channel for INVITE command"); return ERR_NOPRIVILEGES; } - _uTarget = searchList(this->_users, _args.at(2)); + _uTarget = searchList(this->_users, _args.at(1)); if (this->_uTarget == NULL) { WARNING_MSG("User not found"); return ERR_NOSUCHNICK; diff --git a/sources/commands/kick.cpp b/sources/commands/kick.cpp index 0598ffa..47c8e6a 100644 --- a/sources/commands/kick.cpp +++ b/sources/commands/kick.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/24 17:29:48 by rparodi #+# #+# */ -/* Updated: 2025/06/22 19:38:09 by sben-tay ### ########.fr */ +/* Updated: 2025/06/23 15:27:38 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,7 +68,7 @@ void Kick::execute() { if (BONUS && _args.at(2) == "bot") { if (_cTarget->getBotChannel()) { std::string msgKickBot = ":bot!ircbot@localhost KICK #" + _cTarget->getName() + " " + "bot"; - if (_args.size() > 4) + if (_args.size() > 3) msgKickBot += " :" + _args.at(3); msgKickBot += "\r\n"; std::cout << " msgKickBot: " << msgKickBot << std::endl; @@ -80,9 +80,12 @@ void Kick::execute() { std::string msgPart = ":" + this->_uTarget->getPrefix() + " PART #" + _cTarget->getName() + "\r\n"; - std::string msgKick = ":" + this->_uTarget->getPrefix() + " KICK #" + this->_cTarget->getName(); - if (_args.size() > 4) - msgKick += " :" + _args.at(4); + std::string msgKick = ":" + this->_uTarget->getPrefix() + " KICK #" + this->_cTarget->getName() + " " + _uTarget->getName(); + if (_args.size() > 3) + { + DEBUG_MSG("Je rajoute le message de kick avec un motif"); + msgKick += " :" + _args.at(3); + } msgKick += "\r\n"; std::cout << " msgKick: " << msgKick << "msgPart: " << msgPart << std::endl; diff --git a/sources/core/Server.cpp b/sources/core/Server.cpp index 48f7e5b..48ccbe7 100644 --- a/sources/core/Server.cpp +++ b/sources/core/Server.cpp @@ -6,7 +6,7 @@ /* By: sben-tay +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/13 11:11:07 by rparodi #+# #+# */ -/* Updated: 2025/06/23 14:39:03 by sben-tay ### ########.fr */ +/* Updated: 2025/06/23 14:46:53 by sben-tay ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,10 +52,15 @@ Server::Server(int port, const std::string &password) : _port(port), _password(p Server::~Server() { std::cout << CLR_GREY << "Info: Server destructor called" << CLR_RESET << std::endl; - if (_serverFd != -1) - { + if (_serverFd != -1) { close(_serverFd); } + for (std::list::iterator it = _channels.begin(); it != _channels.end(); ++it) { + delete *it; + } + for (std::map::iterator it = _users.begin(); it != _users.end(); ++it) { + delete it->second; + } } std::vector splitLines(const std::string& input);