From 232ff75ae0f3ed235ba5b455ac9383d4c15dc35d Mon Sep 17 00:00:00 2001 From: Albert S Date: Sun, 12 May 2019 21:14:26 +0200 Subject: [PATCH] handlerlogin: use std::atomic for counter and lockguard in ban check --- handlers/handlerlogin.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/handlers/handlerlogin.cpp b/handlers/handlerlogin.cpp index 50caf14..c25fe56 100644 --- a/handlers/handlerlogin.cpp +++ b/handlers/handlerlogin.cpp @@ -18,12 +18,15 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include +#include #include #include "handlerlogin.h" #include "../logger.h" struct LoginFail { - unsigned int count; + std::mutex mutex; + std::atomic count; time_t lastfail; }; static std::map loginFails; @@ -34,6 +37,7 @@ bool HandlerLogin::isBanned(std::string ip) if(utils::hasKey(loginFails, ip)) { LoginFail &fl = loginFails[ip]; + std::lock_guard lock(fl.mutex); return fl.count > 5 && (time(nullptr) - fl.lastfail) < 1200; }