Skip to content

Commit

Permalink
fix: forge history (#3124)
Browse files Browse the repository at this point in the history
This fixes the history forge that is crashing the server trying to
access an index of a empty vector.

Fixes #3121
  • Loading branch information
phacUFPE authored Nov 19, 2024
1 parent 5072de8 commit 7db9c78
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t>(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<ForgeHistory> 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<uint16_t>(std::floor((historyVectorLen - 1) / 9) + 1, 0, std::numeric_limits<uint16_t>::max());
currentPage = (lastPage < page) ? lastPage : page;

pageFirstEntry = std::clamp<uint16_t>(historyVectorLen - (currentPage - 1) * 9, 0, std::numeric_limits<uint16_t>::max());
pageLastEntry = historyVectorLen > currentPage * 9 ? std::clamp<uint16_t>(historyVectorLen - currentPage * 9, 0, std::numeric_limits<uint16_t>::max()) : 0;

for (uint16_t entry = pageFirstEntry; entry > pageLastEntry; --entry) {
historyPerPage.emplace_back(historyVector[entry - 1]);
}
}

auto historyPageToSend = historyPerPage.size();
Expand Down

0 comments on commit 7db9c78

Please sign in to comment.