opentibiabr/canary

void ProtocolGame::sendInventoryIds

Closed this issue · 3 comments

Priority

Critical

Area

  • Datapack
  • Source
  • Map
  • Other

What happened?

When my server starts up it crashes after a while.

SRC is current without modifications.

CHANGELOG CRASH
2024-04-01 19-29-48.log

What OS are you seeing the problem on?

Linux

Code of Conduct

  • I agree to follow this project's Code of Conduct

Okay, change your function:
void ProtocolGame::sendInventoryIds() {
And try to reproduce this crash again.

to:

bool ProtocolGame::sendInventoryIds() {
    if (!player) {
        return false;
    }

    ItemsTierCountList items = player->getInventoryItemsId();

    NetworkMessage msg;
    msg.addByte(0xF5);
    auto countPosition = msg.getBufferPosition();
    msg.skipBytes(2); // Total items count

    for (uint16_t i = 1; i <= 11; i++) {
        msg.add<uint16_t>(i);
        msg.addByte(0x00);
        msg.add<uint16_t>(0x01);
    }

    uint16_t totalItemsCount = 0;
    for (const auto &[itemId, item] : items) {
        for (const auto [tier, count] : item) {
            msg.add<uint16_t>(itemId);
            msg.addByte(tier);
            msg.add<uint16_t>(static_cast<uint16_t>(count));
            totalItemsCount++;
        }
    }

    msg.setBufferPosition(countPosition);
    msg.add<uint16_t>(totalItemsCount + 11);
    writeToOutputBuffer(msg);
    return true;
}

don't forget to change in protocolgame.hpp
void sendInventoryIds();
to
bool sendInventoryIds();

_If this solve your problem ok, if not please tell us why to reproduce this._

Okay, change your function: void ProtocolGame::sendInventoryIds() { And try to reproduce this crash again.

to:

bool ProtocolGame::sendInventoryIds() {
    if (!player) {
        return false;
    }

    ItemsTierCountList items = player->getInventoryItemsId();

    NetworkMessage msg;
    msg.addByte(0xF5);
    auto countPosition = msg.getBufferPosition();
    msg.skipBytes(2); // Total items count

    for (uint16_t i = 1; i <= 11; i++) {
        msg.add<uint16_t>(i);
        msg.addByte(0x00);
        msg.add<uint16_t>(0x01);
    }

    uint16_t totalItemsCount = 0;
    for (const auto &[itemId, item] : items) {
        for (const auto [tier, count] : item) {
            msg.add<uint16_t>(itemId);
            msg.addByte(tier);
            msg.add<uint16_t>(static_cast<uint16_t>(count));
            totalItemsCount++;
        }
    }

    msg.setBufferPosition(countPosition);
    msg.add<uint16_t>(totalItemsCount + 11);
    writeToOutputBuffer(msg);
    return true;
}

don't forget to change in protocolgame.hpp void sendInventoryIds(); to bool sendInventoryIds();

_If this solve your problem ok, if not please tell us why to reproduce this._

image

It a unique player when loggin..

-=-=
When I removed the items from the character it started working normally again.

-=-=-=
image

From the log it appears to have been an overflow