Skip to content

Commit

Permalink
Validate player/item names coming from save file
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenCWills authored and AJenbo committed Mar 2, 2025
1 parent c798253 commit 234606a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Source/loadsave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ T SwapBE(T in)
}
}

void TerminateUtf8(char *str, size_t maxLength)
{
std::string_view inStr { str, maxLength };
std::string_view truncStr = TruncateUtf8(inStr, maxLength - 1);
size_t utf8Length = truncStr.size();
str[utf8Length] = '\0';
}

class LoadHelper {
std::unique_ptr<std::byte[]> m_buffer_;
size_t m_cur_ = 0;
Expand Down Expand Up @@ -269,7 +277,9 @@ void LoadItemData(LoadHelper &file, Item &item)
item._iIdentified = file.NextBool32();
item._iMagical = static_cast<item_quality>(file.NextLE<int8_t>());
file.NextBytes(item._iName, ItemNameLength);
TerminateUtf8(item._iName, ItemNameLength);
file.NextBytes(item._iIName, ItemNameLength);
TerminateUtf8(item._iIName, ItemNameLength);
item._iLoc = static_cast<item_equip_type>(file.NextLE<int8_t>());
item._iClass = static_cast<item_class>(file.NextLE<uint8_t>());
file.Skip(1); // Alignment
Expand Down Expand Up @@ -439,6 +449,7 @@ void LoadPlayer(LoadHelper &file, Player &player)
player._pLvlChanging = file.NextBool8();

file.NextBytes(player._pName, PlayerNameLength);
TerminateUtf8(player._pName, PlayerNameLength);
player._pClass = static_cast<HeroClass>(file.NextLE<int8_t>());
file.Skip(3); // Alignment
player._pStrength = file.NextLE<int32_t>();
Expand Down

0 comments on commit 234606a

Please sign in to comment.