Skip to content
Permalink
Browse files

(se)SerializeString: Include max length in the name

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 947466ab28129fd69e6630974c6c4e901f2bebc6
@@ -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);
@@ -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);
}

@@ -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) {
@@ -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);

@@ -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;
}
@@ -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") + "\"");
@@ -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"
@@ -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);
}
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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);
@@ -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)
@@ -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;
@@ -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.
@@ -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)
@@ -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
@@ -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);
}
}
@@ -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);
@@ -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).
@@ -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,
@@ -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);
}
}

@@ -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;
}
@@ -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);
@@ -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);

@@ -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 {

0 comments on commit 947466a

Please sign in to comment.
You can’t perform that action at this time.