From 7db9c7832f08cc05e310e797631099703d5bb2ab Mon Sep 17 00:00:00 2001 From: Pedro Cruz Date: Mon, 18 Nov 2024 23:03:49 -0300 Subject: [PATCH] fix: forge history (#3124) This fixes the history forge that is crashing the server trying to access an index of a empty vector. Fixes #3121 --- src/server/network/protocol/protocolgame.cpp | 21 ++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/server/network/protocol/protocolgame.cpp b/src/server/network/protocol/protocolgame.cpp index 38f7600310e..2f7ff9e2479 100644 --- a/src/server/network/protocol/protocolgame.cpp +++ b/src/server/network/protocol/protocolgame.cpp @@ -5745,14 +5745,23 @@ void ProtocolGame::sendForgeHistory(uint8_t page) { page = page + 1; auto historyVector = player->getForgeHistory(); auto historyVectorLen = historyVector.size(); - uint16_t lastPage = (1 < std::floor((historyVectorLen - 1) / 9) + 1) ? static_cast(std::floor((historyVectorLen - 1) / 9) + 1) : 1; - uint16_t currentPage = (lastPage < page) ? lastPage : page; + + uint16_t currentPage = 1; + uint16_t lastPage = 1; + uint16_t pageFirstEntry = 0; + uint16_t pageLastEntry = 0; std::vector historyPerPage; - uint16_t pageFirstEntry = (0 < historyVectorLen - (currentPage - 1) * 9) ? historyVectorLen - (currentPage - 1) * 9 : 0; - uint16_t pageLastEntry = (0 < historyVectorLen - currentPage * 9) ? historyVectorLen - currentPage * 9 : 0; - for (uint16_t entry = pageFirstEntry; entry > pageLastEntry; --entry) { - historyPerPage.emplace_back(historyVector[entry - 1]); + if (historyVectorLen > 0) { + lastPage = std::clamp(std::floor((historyVectorLen - 1) / 9) + 1, 0, std::numeric_limits::max()); + currentPage = (lastPage < page) ? lastPage : page; + + pageFirstEntry = std::clamp(historyVectorLen - (currentPage - 1) * 9, 0, std::numeric_limits::max()); + pageLastEntry = historyVectorLen > currentPage * 9 ? std::clamp(historyVectorLen - currentPage * 9, 0, std::numeric_limits::max()) : 0; + + for (uint16_t entry = pageFirstEntry; entry > pageLastEntry; --entry) { + historyPerPage.emplace_back(historyVector[entry - 1]); + } } auto historyPageToSend = historyPerPage.size();