Fix memory leak on successive call to setAuthentication

(as reported in https://github.com/tbnobody/OpenDTU/pull/2320)
This commit is contained in:
Mathieu Carbou
2024-10-01 11:11:27 +02:00
parent 6fb4067f8e
commit 041565a7d7
2 changed files with 9 additions and 8 deletions

View File

@@ -717,7 +717,8 @@ class AsyncWebRewrite {
class AsyncWebHandler : public AsyncMiddlewareChain { class AsyncWebHandler : public AsyncMiddlewareChain {
protected: protected:
ArRequestFilterFunction _filter{nullptr}; ArRequestFilterFunction _filter = nullptr;
AuthenticationMiddleware* _authMiddleware = nullptr;
public: public:
AsyncWebHandler() {} AsyncWebHandler() {}

View File

@@ -28,13 +28,13 @@ AsyncWebHandler& AsyncWebHandler::setFilter(ArRequestFilterFunction fn) {
return *this; return *this;
} }
AsyncWebHandler& AsyncWebHandler::setAuthentication(const char* username, const char* password) { AsyncWebHandler& AsyncWebHandler::setAuthentication(const char* username, const char* password) {
if (username == nullptr || password == nullptr || strlen(username) == 0 || strlen(password) == 0) if (!_authMiddleware) {
return *this; _authMiddleware = new AuthenticationMiddleware();
AuthenticationMiddleware* m = new AuthenticationMiddleware(); _authMiddleware->_freeOnRemoval = true;
m->setUsername(username); addMiddleware(_authMiddleware);
m->setPassword(password); }
m->_freeOnRemoval = true; _authMiddleware->setUsername(username);
addMiddleware(m); _authMiddleware->setPassword(password);
return *this; return *this;
}; };