Skip to content

Commit

Permalink
(se)SerializeString: Include max length in the name
Browse files Browse the repository at this point in the history
This commit clarifies the maximal length of the serialized strings.
It will avoid accidental use of serializeString() when a larger string can be expected.

Removes unused Wide String serialization functions
  • Loading branch information
SmallJoker authored and nerzhul committed Oct 1, 2020
1 parent ca5c2db commit 947466a
Show file tree
Hide file tree
Showing 23 changed files with 168 additions and 223 deletions.
12 changes: 6 additions & 6 deletions src/client/content_cao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ void GenericCAO::processInitData(const std::string &data)
}

// PROTOCOL_VERSION >= 37
m_name = deSerializeString(is);
m_name = deSerializeString16(is);
m_is_player = readU8(is);
m_id = readU16(is);
m_position = readV3F32(is);
Expand All @@ -381,7 +381,7 @@ void GenericCAO::processInitData(const std::string &data)
const u8 num_messages = readU8(is);

for (int i = 0; i < num_messages; i++) {
std::string message = deSerializeLongString(is);
std::string message = deSerializeString32(is);
processMessage(message);
}

Expand Down Expand Up @@ -1657,7 +1657,7 @@ void GenericCAO::processMessage(const std::string &data)
rot_translator.update(m_rotation, false, update_interval);
updateNodePos();
} else if (cmd == AO_CMD_SET_TEXTURE_MOD) {
std::string mod = deSerializeString(is);
std::string mod = deSerializeString16(is);

// immediately reset a engine issued texture modifier if a mod sends a different one
if (m_reset_textures_timer > 0) {
Expand Down Expand Up @@ -1735,15 +1735,15 @@ void GenericCAO::processMessage(const std::string &data)
m_animation_speed = readF32(is);
updateAnimationSpeed();
} else if (cmd == AO_CMD_SET_BONE_POSITION) {
std::string bone = deSerializeString(is);
std::string bone = deSerializeString16(is);
v3f position = readV3F32(is);
v3f rotation = readV3F32(is);
m_bone_position[bone] = core::vector2d<v3f>(position, rotation);

// updateBonePosition(); now called every step
} else if (cmd == AO_CMD_ATTACH_TO) {
u16 parent_id = readS16(is);
std::string bone = deSerializeString(is);
std::string bone = deSerializeString16(is);
v3f position = readV3F32(is);
v3f rotation = readV3F32(is);

Expand Down Expand Up @@ -1793,7 +1793,7 @@ void GenericCAO::processMessage(const std::string &data)
int armor_groups_size = readU16(is);
for(int i=0; i<armor_groups_size; i++)
{
std::string name = deSerializeString(is);
std::string name = deSerializeString16(is);
int rating = readS16(is);
m_armor_groups[name] = rating;
}
Expand Down
18 changes: 9 additions & 9 deletions src/content_nodemeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,26 @@ static bool content_nodemeta_deserialize_legacy_body(
if(id == NODEMETA_GENERIC) // GenericNodeMetadata (0.4-dev)
{
meta->getInventory()->deSerialize(is);
deSerializeLongString(is); // m_text
deSerializeString(is); // m_owner
deSerializeString32(is); // m_text
deSerializeString16(is); // m_owner

meta->setString("infotext",deSerializeString(is));
meta->setString("formspec",deSerializeString(is));
meta->setString("infotext",deSerializeString16(is));
meta->setString("formspec",deSerializeString16(is));
readU8(is); // m_allow_text_input
readU8(is); // m_allow_removal
readU8(is); // m_enforce_owner

int num_vars = readU32(is);
for(int i=0; i<num_vars; i++){
std::string name = deSerializeString(is);
std::string var = deSerializeLongString(is);
std::string name = deSerializeString16(is);
std::string var = deSerializeString32(is);
meta->setString(name, var);
}
return false;
}
else if(id == NODEMETA_SIGN) // SignNodeMetadata
{
meta->setString("text", deSerializeString(is));
meta->setString("text", deSerializeString16(is));
//meta->setString("infotext","\"${text}\"");
meta->setString("infotext",
std::string("\"") + meta->getString("text") + "\"");
Expand All @@ -87,7 +87,7 @@ static bool content_nodemeta_deserialize_legacy_body(
}
else if(id == NODEMETA_LOCKABLE_CHEST) // LockingChestNodeMetadata
{
meta->setString("owner", deSerializeString(is));
meta->setString("owner", deSerializeString16(is));
meta->getInventory()->deSerialize(is);

// Rename inventory list "0" to "main"
Expand Down Expand Up @@ -138,7 +138,7 @@ static bool content_nodemeta_deserialize_legacy_meta(
s16 id = readS16(is);

// Read data
std::string data = deSerializeString(is);
std::string data = deSerializeString16(is);
std::istringstream tmp_is(data, std::ios::binary);
return content_nodemeta_deserialize_legacy_body(tmp_is, id, meta);
}
Expand Down
16 changes: 8 additions & 8 deletions src/database/database-leveldb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ void PlayerDatabaseLevelDB::savePlayer(RemotePlayer *player)
StringMap stringvars = sao->getMeta().getStrings();
writeU32(os, stringvars.size());
for (const auto &it : stringvars) {
os << serializeString(it.first);
os << serializeLongString(it.second);
os << serializeString16(it.first);
os << serializeString32(it.second);
}

player->inventory.serialize(os);
Expand Down Expand Up @@ -183,8 +183,8 @@ bool PlayerDatabaseLevelDB::loadPlayer(RemotePlayer *player, PlayerSAO *sao)

u32 attribute_count = readU32(is);
for (u32 i = 0; i < attribute_count; i++) {
std::string name = deSerializeString(is);
std::string value = deSerializeLongString(is);
std::string name = deSerializeString16(is);
std::string value = deSerializeString32(is);
sao->getMeta().setString(name, value);
}
sao->getMeta().setModified(false);
Expand Down Expand Up @@ -247,13 +247,13 @@ bool AuthDatabaseLevelDB::getAuth(const std::string &name, AuthEntry &res)

res.id = 1;
res.name = name;
res.password = deSerializeString(is);
res.password = deSerializeString16(is);

u16 privilege_count = readU16(is);
res.privileges.clear();
res.privileges.reserve(privilege_count);
for (u16 i = 0; i < privilege_count; i++) {
res.privileges.push_back(deSerializeString(is));
res.privileges.push_back(deSerializeString16(is));
}

res.last_login = readS64(is);
Expand All @@ -264,14 +264,14 @@ bool AuthDatabaseLevelDB::saveAuth(const AuthEntry &authEntry)
{
std::ostringstream os;
writeU8(os, 1);
os << serializeString(authEntry.password);
os << serializeString16(authEntry.password);

size_t privilege_count = authEntry.privileges.size();
FATAL_ERROR_IF(privilege_count > U16_MAX,
"Unsupported number of privileges");
writeU16(os, privilege_count);
for (const std::string &privilege : authEntry.privileges) {
os << serializeString(privilege);
os << serializeString16(privilege);
}

writeS64(os, authEntry.last_login);
Expand Down
52 changes: 26 additions & 26 deletions src/itemdef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
u8 version = 6;
writeU8(os, version);
writeU8(os, type);
os << serializeString(name);
os << serializeString(description);
os << serializeString(inventory_image);
os << serializeString(wield_image);
os << serializeString16(name);
os << serializeString16(description);
os << serializeString16(inventory_image);
os << serializeString16(wield_image);
writeV3F32(os, wield_scale);
writeS16(os, stack_max);
writeU8(os, usable);
Expand All @@ -143,25 +143,25 @@ void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
tool_capabilities->serialize(tmp_os, protocol_version);
tool_capabilities_s = tmp_os.str();
}
os << serializeString(tool_capabilities_s);
os << serializeString16(tool_capabilities_s);

writeU16(os, groups.size());
for (const auto &group : groups) {
os << serializeString(group.first);
os << serializeString16(group.first);
writeS16(os, group.second);
}

os << serializeString(node_placement_prediction);
os << serializeString16(node_placement_prediction);

// Version from ContentFeatures::serialize to keep in sync
sound_place.serialize(os, CONTENTFEATURES_VERSION);
sound_place_failed.serialize(os, CONTENTFEATURES_VERSION);

writeF32(os, range);
os << serializeString(palette_image);
os << serializeString16(palette_image);
writeARGB8(os, color);
os << serializeString(inventory_overlay);
os << serializeString(wield_overlay);
os << serializeString16(inventory_overlay);
os << serializeString16(wield_overlay);
}

void ItemDefinition::deSerialize(std::istream &is)
Expand All @@ -175,16 +175,16 @@ void ItemDefinition::deSerialize(std::istream &is)
throw SerializationError("unsupported ItemDefinition version");

type = (enum ItemType)readU8(is);
name = deSerializeString(is);
description = deSerializeString(is);
inventory_image = deSerializeString(is);
wield_image = deSerializeString(is);
name = deSerializeString16(is);
description = deSerializeString16(is);
inventory_image = deSerializeString16(is);
wield_image = deSerializeString16(is);
wield_scale = readV3F32(is);
stack_max = readS16(is);
usable = readU8(is);
liquids_pointable = readU8(is);

std::string tool_capabilities_s = deSerializeString(is);
std::string tool_capabilities_s = deSerializeString16(is);
if (!tool_capabilities_s.empty()) {
std::istringstream tmp_is(tool_capabilities_s, std::ios::binary);
tool_capabilities = new ToolCapabilities;
Expand All @@ -194,22 +194,22 @@ void ItemDefinition::deSerialize(std::istream &is)
groups.clear();
u32 groups_size = readU16(is);
for(u32 i=0; i<groups_size; i++){
std::string name = deSerializeString(is);
std::string name = deSerializeString16(is);
int value = readS16(is);
groups[name] = value;
}

node_placement_prediction = deSerializeString(is);
node_placement_prediction = deSerializeString16(is);

// Version from ContentFeatures::serialize to keep in sync
sound_place.deSerialize(is, CONTENTFEATURES_VERSION);
sound_place_failed.deSerialize(is, CONTENTFEATURES_VERSION);

range = readF32(is);
palette_image = deSerializeString(is);
palette_image = deSerializeString16(is);
color = readARGB8(is);
inventory_overlay = deSerializeString(is);
wield_overlay = deSerializeString(is);
inventory_overlay = deSerializeString16(is);
wield_overlay = deSerializeString16(is);

// If you add anything here, insert it primarily inside the try-catch
// block to not need to increase the version.
Expand Down Expand Up @@ -521,14 +521,14 @@ class CItemDefManager: public IWritableItemDefManager
// Serialize ItemDefinition and write wrapped in a string
std::ostringstream tmp_os(std::ios::binary);
def->serialize(tmp_os, protocol_version);
os << serializeString(tmp_os.str());
os << serializeString16(tmp_os.str());
}

writeU16(os, m_aliases.size());

for (const auto &it : m_aliases) {
os << serializeString(it.first);
os << serializeString(it.second);
os << serializeString16(it.first);
os << serializeString16(it.second);
}
}
void deSerialize(std::istream &is)
Expand All @@ -543,7 +543,7 @@ class CItemDefManager: public IWritableItemDefManager
for(u16 i=0; i<count; i++)
{
// Deserialize a string and grab an ItemDefinition from it
std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
std::istringstream tmp_is(deSerializeString16(is), std::ios::binary);
ItemDefinition def;
def.deSerialize(tmp_is);
// Register
Expand All @@ -552,8 +552,8 @@ class CItemDefManager: public IWritableItemDefManager
u16 num_aliases = readU16(is);
for(u16 i=0; i<num_aliases; i++)
{
std::string name = deSerializeString(is);
std::string convert_to = deSerializeString(is);
std::string name = deSerializeString16(is);
std::string convert_to = deSerializeString16(is);
registerAlias(name, convert_to);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/mapblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,13 +668,13 @@ void MapBlock::deSerialize_pre22(std::istream &is, u8 version, bool disk)
// Ignore errors
try {
if (version <= 15) {
std::string data = deSerializeString(is);
std::string data = deSerializeString16(is);
std::istringstream iss(data, std::ios_base::binary);
content_nodemeta_deserialize_legacy(iss,
&m_node_metadata, &m_node_timers,
m_gamedef->idef());
} else {
//std::string data = deSerializeLongString(is);
//std::string data = deSerializeString32(is);
std::ostringstream oss(std::ios_base::binary);
decompressZlib(is, oss);
std::istringstream iss(oss.str(), std::ios_base::binary);
Expand Down
4 changes: 2 additions & 2 deletions src/mapgen/mg_schematic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ bool Schematic::deserializeFromMts(std::istream *is,
//// Read node names
u16 nidmapcount = readU16(ss);
for (int i = 0; i != nidmapcount; i++) {
std::string name = deSerializeString(ss);
std::string name = deSerializeString16(ss);

// Instances of "ignore" from v1 are converted to air (and instances
// are fixed to have MTSCHEM_PROB_NEVER later on).
Expand Down Expand Up @@ -372,7 +372,7 @@ bool Schematic::serializeToMts(std::ostream *os,

writeU16(ss, names.size()); // name count
for (size_t i = 0; i != names.size(); i++)
ss << serializeString(names[i]); // node names
ss << serializeString16(names[i]); // node names

// compressed bulk node data
MapNode::serializeBulk(ss, SER_FMT_VER_HIGHEST_WRITE,
Expand Down
4 changes: 2 additions & 2 deletions src/nameidmapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void NameIdMapping::serialize(std::ostream &os) const
writeU16(os, m_id_to_name.size());
for (const auto &i : m_id_to_name) {
writeU16(os, i.first);
os << serializeString(i.second);
os << serializeString16(i.second);
}
}

Expand All @@ -41,7 +41,7 @@ void NameIdMapping::deSerialize(std::istream &is)
m_name_to_id.clear();
for (u32 i = 0; i < count; i++) {
u16 id = readU16(is);
std::string name = deSerializeString(is);
std::string name = deSerializeString16(is);
m_id_to_name[id] = name;
m_name_to_id[name] = id;
}
Expand Down
8 changes: 4 additions & 4 deletions src/network/clientpackethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ void Client::handleCommand_ActiveObjectMessages(NetworkPacket* pkt)
if (!is.good())
break;

std::string message = deSerializeString(is);
std::string message = deSerializeString16(is);

// Pass on to the environment
m_env.processActiveObjectMessage(id, message);
Expand Down Expand Up @@ -994,7 +994,7 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt)
p.minsize = readF32(is);
p.maxsize = readF32(is);
p.collisiondetection = readU8(is);
p.texture = deSerializeLongString(is);
p.texture = deSerializeString32(is);

server_id = readU32(is);

Expand Down Expand Up @@ -1207,11 +1207,11 @@ void Client::handleCommand_HudSetSky(NetworkPacket* pkt)

SkyboxParams skybox;
skybox.bgcolor = video::SColor(readARGB8(is));
skybox.type = std::string(deSerializeString(is));
skybox.type = std::string(deSerializeString16(is));
u16 count = readU16(is);

for (size_t i = 0; i < count; i++)
skybox.textures.emplace_back(deSerializeString(is));
skybox.textures.emplace_back(deSerializeString16(is));

skybox.clouds = true;
try {
Expand Down
Loading

0 comments on commit 947466a

Please sign in to comment.