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 {
protected:
ArRequestFilterFunction _filter{nullptr};
ArRequestFilterFunction _filter = nullptr;
AuthenticationMiddleware* _authMiddleware = nullptr;
public:
AsyncWebHandler() {}

View File

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