🔨」 fix: corrected invalid read and leak

This commit is contained in:
2025-04-25 15:22:13 +02:00
parent 4ba80434e6
commit 6a1bcaad05
5 changed files with 47 additions and 41 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */
/* Updated: 2025/04/25 14:33:54 by adjoly ### ########.fr */
/* Updated: 2025/04/25 15:15:48 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -37,9 +37,14 @@ class Client {
return true;
}
bool isToClose() {
return toClose;
}
private:
void _getRequest(std::string);
bool toClose;
struct pollfd *_pfd;
struct sockaddr_in _client_addr;
http::IRequest *_request;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */
/* Updated: 2025/04/25 14:52:50 by adjoly ### ########.fr */
/* Updated: 2025/04/25 15:16:52 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -41,13 +41,6 @@ class Server {
*/
void _run(void);
/**
* @brief Used to handle client request
*
* @param the position in the _client_data
*/
bool _handle_client(Client *);
/**
* @brief Can be used to fill the vector passed as parameters with all the
* port and host in the config
@ -80,6 +73,8 @@ class Server {
Client *_getClient(int);
void _destroy_clients(void);
config::Config
*_conf; // Pointer to the configuration class (with all config in)
Logger *_log; // Pointer to the log class

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/04/25 14:35:37 by adjoly ### ########.fr */
/* Updated: 2025/04/25 15:19:43 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -83,6 +83,6 @@ void Client::answer(void) {
Client::~Client(void) {
log("", "Client", "destructor called");
delete _pfd;
//delete _pfd;
delete (http::Get *)(this->_request);
}

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
/* Updated: 2025/04/25 14:54:42 by adjoly ### ########.fr */
/* Updated: 2025/04/25 15:21:43 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -108,6 +108,8 @@ void Server::_run(void) {
// to add signal instead of 727
while (727 - sigHandling()) {
if (poll(_client_fds.data(), _client_fds.size(), -1) < 0) {
if (errno == EINTR)
continue;
std::stringstream str;
str << "poll failed : ";
str << strerror(errno);
@ -136,27 +138,36 @@ void Server::_run(void) {
pfd.events = POLLIN | POLLOUT;
pfd.revents = 0;
_client_fds.push_back(pfd);
struct pollfd *ppfd = _client_fds.data() + _client_fds.size() -1;
struct pollfd *ppfd = _client_fds.data() + _client_fds.size() - 1;
Client *new_client = new Client(ppfd, client_addr, _conf);
_client_data.push_back(new_client);
}
for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) {
if (_client_fds[i].revents & POLLERR) {
close(_client_fds[i].fd);
_client_fds.erase(_client_fds.begin() + i);
delete _client_data[i - _fds_server.size()];
_client_data.erase(_client_data.begin() + i);
i--;
} else if (_client_fds[i].revents & POLLIN) {
Client *client = _getClient(_client_fds[i].fd);
if (client == not_nullptr) {
_log->error("client does not exist");
continue;
}
if (_client_fds[i].revents & POLLIN) {
if (_handle_client(client)) {
close(_client_fds[i].fd);
_client_fds.erase(_client_fds.begin() + i);
delete _client_data[i];
_client_data.erase(_client_data.begin() + i);
i--;
}
client->parse();
} else if (_client_fds[i].revents & POLLOUT) {
Client *client = _getClient(_client_fds[i].fd);
if (client == not_nullptr) {
_log->error("client does not exist");
continue;
}
client->answer();
}
}
_destroy_clients();
}
}

View File

@ -6,12 +6,12 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */
/* Updated: 2025/04/25 14:53:17 by adjoly ### ########.fr */
/* Updated: 2025/04/25 15:17:55 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <server/Client.hpp>
#include <netinet/in.h>
#include <server/Client.hpp>
#include <server/default.hpp>
#include <sstream>
#include <stdexcept>
@ -74,22 +74,17 @@ int Server::_createSocket(std::string host, int port) {
close(fd);
std::ostringstream str;
str << port;
throw std::runtime_error("listen failed for : " + host + ":" + str.str());
throw std::runtime_error("listen failed for : " + host + ":" +
str.str());
}
return (fd);
}
bool Server::_handle_client(Client *client) {
try {
client->parse();
client->answer();
} catch (std::runtime_error &e) {
_log->error(e.what());
return false;
void Server::_destroy_clients() {
for (auto it = range(_client_data)) {
if ((*it)->isToClose()) {
delete (*it);
}
}
return true;
}