refactor: add Packet::hasData(), replace 52 position checks and 14 more Lua guards

Add Packet::hasData() for 'has remaining data' checks, replacing 52
verbose getReadPos()<getSize() comparisons across 3 files. Also replace
14 more standalone Lua return patterns with luaReturnNil/Zero helpers.
This commit is contained in:
Kelsi 2026-03-25 14:39:01 -07:00
parent 4c26b1a8ae
commit 12355316b3
5 changed files with 67 additions and 66 deletions

View file

@ -1638,7 +1638,7 @@ void GameHandler::registerOpcodeHandlers() {
dispatchTable_[Opcode::SMSG_PLAYED_TIME] = [this](network::Packet& packet) { if (state == WorldState::IN_WORLD) handlePlayedTime(packet); };
dispatchTable_[Opcode::SMSG_WHO] = [this](network::Packet& packet) { if (state == WorldState::IN_WORLD) handleWho(packet); };
dispatchTable_[Opcode::SMSG_WHOIS] = [this](network::Packet& packet) {
if (packet.getReadPos() < packet.getSize()) {
if (packet.hasData()) {
std::string whoisText = packet.readString();
if (!whoisText.empty()) {
std::string line;
@ -2846,7 +2846,7 @@ void GameHandler::registerOpcodeHandlers() {
if (packet.getRemainingSize() < 8) return;
uint64_t casterGuid = packet.readUInt64();
std::string casterName;
if (packet.getReadPos() < packet.getSize())
if (packet.hasData())
casterName = packet.readString();
if (casterGuid) {
resurrectCasterGuid_ = casterGuid;
@ -3188,7 +3188,7 @@ void GameHandler::registerOpcodeHandlers() {
dispatchTable_[Opcode::SMSG_NEW_WORLD] = [this](network::Packet& packet) { handleNewWorld(packet); };
dispatchTable_[Opcode::SMSG_TRANSFER_ABORTED] = [this](network::Packet& packet) {
uint32_t mapId = packet.readUInt32();
uint8_t reason = (packet.getReadPos() < packet.getSize()) ? packet.readUInt8() : 0;
uint8_t reason = (packet.hasData()) ? packet.readUInt8() : 0;
(void)mapId;
const char* abortMsg = nullptr;
switch (reason) {
@ -3504,7 +3504,7 @@ void GameHandler::registerOpcodeHandlers() {
// Group set leader
dispatchTable_[Opcode::SMSG_GROUP_SET_LEADER] = [this](network::Packet& packet) {
if (packet.getSize() <= packet.getReadPos()) return;
if (!packet.hasData()) return;
std::string leaderName = packet.readString();
for (const auto& m : partyData.members) {
if (m.name == leaderName) { partyData.leaderGuid = m.guid; break; }
@ -5572,7 +5572,7 @@ void GameHandler::registerOpcodeHandlers() {
packet.skipAll();
};
dispatchTable_[Opcode::SMSG_GUILD_DECLINE] = [this](network::Packet& packet) {
if (packet.getReadPos() < packet.getSize()) {
if (packet.hasData()) {
std::string name = packet.readString();
addSystemChatMessage(name + " declined your guild invitation.");
}
@ -6600,7 +6600,7 @@ void GameHandler::registerOpcodeHandlers() {
// charName (cstring) + guid (uint64) + achievementId (uint32) + ...
dispatchTable_[Opcode::SMSG_SERVER_FIRST_ACHIEVEMENT] = [this](network::Packet& packet) {
// charName (cstring) + guid (uint64) + achievementId (uint32) + ...
if (packet.getReadPos() < packet.getSize()) {
if (packet.hasData()) {
std::string charName = packet.readString();
if (packet.getRemainingSize() >= 12) {
/*uint64_t guid =*/ packet.readUInt64();
@ -7386,7 +7386,7 @@ void GameHandler::registerOpcodeHandlers() {
}
/*uint32_t command =*/ packet.readUInt32();
uint8_t result = packet.readUInt8();
std::string info = (packet.getReadPos() < packet.getSize()) ? packet.readString() : "";
std::string info = (packet.hasData()) ? packet.readString() : "";
if (result != 0) {
// Map common calendar error codes to friendly strings
static const char* kCalendarErrors[] = {
@ -7425,7 +7425,7 @@ void GameHandler::registerOpcodeHandlers() {
packet.skipAll(); return;
}
/*uint64_t eventId =*/ packet.readUInt64();
std::string title = (packet.getReadPos() < packet.getSize()) ? packet.readString() : "";
std::string title = (packet.hasData()) ? packet.readString() : "";
packet.skipAll(); // consume remaining fields
if (!title.empty()) {
addSystemChatMessage("Calendar invite: " + title);
@ -7453,7 +7453,7 @@ void GameHandler::registerOpcodeHandlers() {
uint8_t status = packet.readUInt8();
/*uint8_t rank =*/ packet.readUInt8();
/*uint8_t isGuild =*/ packet.readUInt8();
std::string evTitle = (packet.getReadPos() < packet.getSize()) ? packet.readString() : "";
std::string evTitle = (packet.hasData()) ? packet.readString() : "";
// status: 0=Invited,1=Accepted,2=Declined,3=Confirmed,4=Out,5=Standby,6=SignedUp,7=Not Signed Up,8=Tentative
static const char* kRsvpStatus[] = {
"invited", "accepted", "declined", "confirmed",
@ -7533,7 +7533,7 @@ void GameHandler::registerOpcodeHandlers() {
uint64_t kickerGuid = packet.readUInt64();
uint32_t reasonType = packet.readUInt32();
std::string reason;
if (packet.getReadPos() < packet.getSize())
if (packet.hasData())
reason = packet.readString();
(void)kickerGuid;
(void)reasonType;
@ -7578,14 +7578,14 @@ void GameHandler::registerOpcodeHandlers() {
uint32_t ticketId = packet.readUInt32();
std::string subject;
std::string body;
if (packet.getReadPos() < packet.getSize()) subject = packet.readString();
if (packet.getReadPos() < packet.getSize()) body = packet.readString();
if (packet.hasData()) subject = packet.readString();
if (packet.hasData()) body = packet.readString();
uint32_t responseCount = 0;
if (packet.hasRemaining(4))
responseCount = packet.readUInt32();
std::string responseText;
for (uint32_t i = 0; i < responseCount && i < 10; ++i) {
if (packet.getReadPos() < packet.getSize()) {
if (packet.hasData()) {
std::string t = packet.readString();
if (i == 0) responseText = t;
}
@ -16519,9 +16519,9 @@ void GameHandler::handleLfgBootProposalUpdate(network::Packet& packet) {
lfgBootNeeded_ = votesNeeded;
// Optional: reason string and target name (null-terminated) follow the fixed fields
if (packet.getReadPos() < packet.getSize())
if (packet.hasData())
lfgBootReason_ = packet.readString();
if (packet.getReadPos() < packet.getSize())
if (packet.hasData())
lfgBootTargetName_ = packet.readString();
if (inProgress) {
@ -23516,7 +23516,7 @@ void GameHandler::handleWho(network::Packet& packet) {
}
for (uint32_t i = 0; i < displayCount; ++i) {
if (packet.getReadPos() >= packet.getSize()) break;
if (!packet.hasData()) break;
std::string playerName = packet.readString();
std::string guildName = packet.readString();
if (packet.getRemainingSize() < 12) break;

View file

@ -1246,7 +1246,7 @@ bool ClassicPacketParsers::parseMessageChat(network::Packet& packet, MessageChat
}
// Read chat tag
if (packet.getReadPos() < packet.getSize()) {
if (packet.hasData()) {
data.chatTag = packet.readUInt8();
}
@ -1855,7 +1855,7 @@ bool ClassicPacketParsers::parseItemQueryResponse(network::Packet& packet, ItemQ
data.bindType = packet.readUInt32();
// Description (flavor/lore text)
if (packet.getReadPos() < packet.getSize())
if (packet.hasData())
data.description = packet.readString();
// Post-description: PageText, LanguageID, PageMaterial, StartQuest
@ -2095,7 +2095,7 @@ bool TurtlePacketParsers::parseUpdateObject(network::Packet& packet, UpdateObjec
}
if (withHasTransportByte) {
if (packet.getReadPos() >= packet.getSize()) {
if (!packet.hasData()) {
packet.setReadPos(start);
return false;
}
@ -2122,7 +2122,7 @@ bool TurtlePacketParsers::parseUpdateObject(network::Packet& packet, UpdateObjec
return false;
}
for (uint32_t i = 0; i < count; ++i) {
if (packet.getReadPos() >= packet.getSize()) {
if (!packet.hasData()) {
packet.setReadPos(start);
return false;
}
@ -2136,7 +2136,7 @@ bool TurtlePacketParsers::parseUpdateObject(network::Packet& packet, UpdateObjec
out.blockCount = remainingBlockCount;
out.blocks.reserve(out.blockCount);
for (uint32_t i = 0; i < out.blockCount; ++i) {
if (packet.getReadPos() >= packet.getSize()) {
if (!packet.hasData()) {
packet.setReadPos(start);
return false;
}
@ -2167,7 +2167,7 @@ bool TurtlePacketParsers::parseUpdateObject(network::Packet& packet, UpdateObjec
case UpdateType::CREATE_OBJECT:
case UpdateType::CREATE_OBJECT2:
block.guid = packet.readPackedGuid();
if (packet.getReadPos() >= packet.getSize()) return false;
if (!packet.hasData()) return false;
block.objectType = static_cast<ObjectType>(packet.readUInt8());
if (!movementParser(packet, block)) return false;
if (!UpdateObjectParser::parseUpdateFields(packet, block)) return false;

View file

@ -21,7 +21,7 @@ namespace {
}
bool hasFullPackedGuid(const wowee::network::Packet& packet) {
if (packet.getReadPos() >= packet.getSize()) {
if (!packet.hasData()) {
return false;
}
@ -451,7 +451,7 @@ bool CharEnumParser::parse(network::Packet& packet, CharEnumResponse& response)
character.guid = packet.readUInt64();
// Read name (null-terminated string) - validate before reading
if (packet.getReadPos() >= packet.getSize()) {
if (!packet.hasData()) {
LOG_WARNING("CharEnumParser: no bytes for name at index ", static_cast<int>(i));
break;
}
@ -683,7 +683,7 @@ bool MotdParser::parse(network::Packet& packet, MotdData& data) {
for (uint32_t i = 0; i < lineCount; ++i) {
// Validate at least 1 byte available for the string
if (packet.getReadPos() >= packet.getSize()) {
if (!packet.hasData()) {
LOG_WARNING("MotdParser: truncated at line ", i + 1);
break;
}
@ -1154,7 +1154,7 @@ bool UpdateObjectParser::parseMovementBlock(network::Packet& packet, UpdateBlock
bool UpdateObjectParser::parseUpdateFields(network::Packet& packet, UpdateBlock& block) {
size_t startPos = packet.getReadPos();
if (packet.getReadPos() >= packet.getSize()) return false;
if (!packet.hasData()) return false;
// Read number of blocks (each block is 32 fields = 32 bits)
uint8_t blockCount = packet.readUInt8();
@ -1261,7 +1261,7 @@ bool UpdateObjectParser::parseUpdateFields(network::Packet& packet, UpdateBlock&
}
bool UpdateObjectParser::parseUpdateBlock(network::Packet& packet, UpdateBlock& block) {
if (packet.getReadPos() >= packet.getSize()) return false;
if (!packet.hasData()) return false;
// Read update type
uint8_t updateTypeVal = packet.readUInt8();
@ -1272,7 +1272,7 @@ bool UpdateObjectParser::parseUpdateBlock(network::Packet& packet, UpdateBlock&
switch (block.updateType) {
case UpdateType::VALUES: {
// Partial update - changed fields only
if (packet.getReadPos() >= packet.getSize()) return false;
if (!packet.hasData()) return false;
block.guid = packet.readPackedGuid();
LOG_DEBUG(" VALUES update for GUID: 0x", std::hex, block.guid, std::dec);
@ -1291,12 +1291,12 @@ bool UpdateObjectParser::parseUpdateBlock(network::Packet& packet, UpdateBlock&
case UpdateType::CREATE_OBJECT:
case UpdateType::CREATE_OBJECT2: {
// Create new object with full data
if (packet.getReadPos() >= packet.getSize()) return false;
if (!packet.hasData()) return false;
block.guid = packet.readPackedGuid();
LOG_DEBUG(" CREATE_OBJECT for GUID: 0x", std::hex, block.guid, std::dec);
// Read object type
if (packet.getReadPos() >= packet.getSize()) return false;
if (!packet.hasData()) return false;
uint8_t objectTypeVal = packet.readUInt8();
block.objectType = static_cast<ObjectType>(objectTypeVal);
LOG_DEBUG(" Object type: ", static_cast<int>(objectTypeVal));
@ -1421,7 +1421,7 @@ bool DestroyObjectParser::parse(network::Packet& packet, DestroyObjectData& data
data.guid = packet.readUInt64();
// WotLK adds isDeath byte; vanilla/TBC packets are exactly 8 bytes
if (packet.getReadPos() < packet.getSize()) {
if (packet.hasData()) {
data.isDeath = (packet.readUInt8() != 0);
} else {
data.isDeath = false;
@ -1915,7 +1915,7 @@ bool FriendStatusParser::parse(network::Packet& packet, FriendStatusData& data)
data.guid = packet.readUInt64();
if (data.status == 1) { // Online
// Conditional: note (string) + chatFlag (1)
if (packet.getReadPos() < packet.getSize()) {
if (packet.hasData()) {
data.note = packet.readString();
if (packet.getReadPos() + 1 <= packet.getSize()) {
data.chatFlag = packet.readUInt8();
@ -2236,7 +2236,7 @@ bool GuildQueryResponseParser::parse(network::Packet& packet, GuildQueryResponse
data.guildId = packet.readUInt32();
// Validate before reading guild name
if (packet.getReadPos() >= packet.getSize()) {
if (!packet.hasData()) {
LOG_WARNING("GuildQueryResponseParser: truncated before guild name");
data.guildName.clear();
return true;
@ -2245,7 +2245,7 @@ bool GuildQueryResponseParser::parse(network::Packet& packet, GuildQueryResponse
// Read 10 rank names with validation
for (int i = 0; i < 10; ++i) {
if (packet.getReadPos() >= packet.getSize()) {
if (!packet.hasData()) {
LOG_WARNING("GuildQueryResponseParser: truncated at rank name ", i);
data.rankNames[i].clear();
} else {
@ -2358,7 +2358,7 @@ bool GuildRosterParser::parse(network::Packet& packet, GuildRosterData& data) {
m.online = (packet.readUInt8() != 0);
// Validate before reading name string
if (packet.getReadPos() >= packet.getSize()) {
if (!packet.hasData()) {
m.name.clear();
} else {
m.name = packet.readString();
@ -2399,12 +2399,12 @@ bool GuildRosterParser::parse(network::Packet& packet, GuildRosterData& data) {
}
// Read notes
if (packet.getReadPos() >= packet.getSize()) {
if (!packet.hasData()) {
m.publicNote.clear();
m.officerNote.clear();
} else {
m.publicNote = packet.readString();
if (packet.getReadPos() >= packet.getSize()) {
if (!packet.hasData()) {
m.officerNote.clear();
} else {
m.officerNote = packet.readString();
@ -3046,7 +3046,7 @@ bool ItemQueryResponseParser::parse(network::Packet& packet, ItemQueryResponseDa
data.bindType = packet.readUInt32();
// Flavor/lore text (Description cstring)
if (packet.getReadPos() < packet.getSize())
if (packet.hasData())
data.description = packet.readString();
// Post-description fields: PageText, LanguageID, PageMaterial, StartQuest
@ -3094,7 +3094,7 @@ bool MonsterMoveParser::parse(network::Packet& packet, MonsterMoveData& data) {
if (data.guid == 0) return false;
// uint8 unk (toggle for MOVEMENTFLAG2_UNK7)
if (packet.getReadPos() >= packet.getSize()) return false;
if (!packet.hasData()) return false;
packet.readUInt8();
// Current position (server coords: float x, y, z)
@ -3108,7 +3108,7 @@ bool MonsterMoveParser::parse(network::Packet& packet, MonsterMoveData& data) {
packet.readUInt32();
// uint8 moveType
if (packet.getReadPos() >= packet.getSize()) return false;
if (!packet.hasData()) return false;
data.moveType = packet.readUInt8();
if (data.moveType == 1) {
@ -3231,7 +3231,7 @@ bool MonsterMoveParser::parseVanilla(network::Packet& packet, MonsterMoveData& d
if (packet.getReadPos() + 4 > packet.getSize()) return false;
/*uint32_t splineIdOrTick =*/ packet.readUInt32();
if (packet.getReadPos() >= packet.getSize()) return false;
if (!packet.hasData()) return false;
data.moveType = packet.readUInt8();
if (data.moveType == 1) {
@ -3328,7 +3328,7 @@ bool AttackStartParser::parse(network::Packet& packet, AttackStartData& data) {
bool AttackStopParser::parse(network::Packet& packet, AttackStopData& data) {
data.attackerGuid = packet.readPackedGuid();
data.victimGuid = packet.readPackedGuid();
if (packet.getReadPos() < packet.getSize()) {
if (packet.hasData()) {
data.unknown = packet.readUInt32();
}
LOG_DEBUG("Attack stopped: 0x", std::hex, data.attackerGuid, std::dec);
@ -3780,7 +3780,7 @@ bool SpellStartParser::parse(network::Packet& packet, SpellStartData& data) {
}
// STRING: null-terminated
if (targetFlags & 0x0200u) {
while (packet.getReadPos() < packet.getSize() && packet.readUInt8() != 0) {}
while (packet.hasData() && packet.readUInt8() != 0) {}
}
LOG_DEBUG("Spell start: spell=", data.spellId, " castTime=", data.castTime, "ms");
@ -3919,7 +3919,7 @@ bool SpellGoParser::parse(network::Packet& packet, SpellGoData& data) {
// WotLK 3.3.5a SpellCastTargets — consume ALL target payload bytes so that
// any trailing fields after the target section are not misaligned for
// ground-targeted or AoE spells. Same layout as SpellStartParser.
if (packet.getReadPos() < packet.getSize()) {
if (packet.hasData()) {
if (packet.getRemainingSize() >= 4) {
uint32_t targetFlags = packet.readUInt32();
@ -3955,7 +3955,7 @@ bool SpellGoParser::parse(network::Packet& packet, SpellGoData& data) {
}
// STRING: null-terminated
if (targetFlags & 0x0200u) {
while (packet.getReadPos() < packet.getSize() && packet.readUInt8() != 0) {}
while (packet.hasData() && packet.readUInt8() != 0) {}
}
}
}
@ -3975,7 +3975,7 @@ bool AuraUpdateParser::parse(network::Packet& packet, AuraUpdateData& data, bool
uint32_t maxAuras = isAll ? 512 : 1;
uint32_t auraCount = 0;
while (packet.getReadPos() < packet.getSize() && auraCount < maxAuras) {
while (packet.hasData() && auraCount < maxAuras) {
// Validate we can read slot (1) + spellId (4) = 5 bytes minimum
if (packet.getRemainingSize() < 5) {
LOG_DEBUG("Aura update: truncated entry at position ", auraCount);
@ -4043,7 +4043,7 @@ bool AuraUpdateParser::parse(network::Packet& packet, AuraUpdateData& data, bool
if (!isAll) break;
}
if (auraCount >= maxAuras && packet.getReadPos() < packet.getSize()) {
if (auraCount >= maxAuras && packet.hasData()) {
LOG_WARNING("Aura update: capped at ", maxAuras, " entries, remaining data ignored");
}
@ -4995,7 +4995,7 @@ bool TrainerListParser::parse(network::Packet& packet, TrainerListData& data, bo
data.spells.push_back(spell);
}
if (packet.getReadPos() >= packet.getSize()) {
if (!packet.hasData()) {
LOG_WARNING("TrainerListParser: truncated before greeting");
data.greeting.clear();
} else {
@ -5378,7 +5378,7 @@ bool PacketParsers::parseMailList(network::Packet& packet, std::vector<MailMessa
size_t consumed = packet.getReadPos() - startPos;
if (consumed < msgSize) {
size_t skip = msgSize - consumed;
for (size_t s = 0; s < skip && packet.getReadPos() < packet.getSize(); ++s)
for (size_t s = 0; s < skip && packet.hasData(); ++s)
packet.readUInt8();
}
}
@ -5517,12 +5517,12 @@ bool GuildBankListParser::parse(network::Packet& packet, GuildBankData& data) {
data.tabs.resize(tabCount);
for (uint8_t i = 0; i < tabCount; ++i) {
// Validate before reading strings
if (packet.getReadPos() >= packet.getSize()) {
if (!packet.hasData()) {
LOG_WARNING("GuildBankListParser: truncated tab at index ", static_cast<int>(i));
break;
}
data.tabs[i].tabName = packet.readString();
if (packet.getReadPos() >= packet.getSize()) {
if (!packet.hasData()) {
data.tabs[i].tabIcon.clear();
} else {
data.tabs[i].tabIcon = packet.readString();
@ -5606,7 +5606,7 @@ bool AuctionHelloParser::parse(network::Packet& packet, AuctionHelloData& data)
data.auctioneerGuid = packet.readUInt64();
data.auctionHouseId = packet.readUInt32();
// WotLK has an extra uint8 enabled field; Vanilla does not
if (packet.getReadPos() < packet.getSize()) {
if (packet.hasData()) {
data.enabled = packet.readUInt8();
} else {
data.enabled = 1;