1
0
mirror of https://github.com/luanti-org/luanti.git synced 2026-01-12 20:25:26 +01:00

Serialize: Throw exception on incomplete reads (#16796)

Several mistakes were made past where the stream was expected to raise
the EOF flag when reaching the end of stream. That is incorrect. The
flag is only raised if the current read operation fails.

This commit unifies all istream compatibility code to use 'canRead'
for reliable EOF detection. An exception is now thrown when a
value cannot be read completely (e.g. missing bytes).
Version comments are added for easier backtracing.
This commit is contained in:
SmallJoker
2026-01-03 11:13:14 +01:00
committed by GitHub
parent 6079d762ce
commit 3d10d4e859
13 changed files with 207 additions and 179 deletions

View File

@@ -212,10 +212,6 @@ void ServerParticleTexture::deSerialize(std::istream &is, u16 protocol_ver,
{
FlagT flags = 0;
deSerializeParameterValue(is, flags);
// new texture properties were missing in ParticleParameters::serialize
// before Minetest 5.9.0
if (is.eof())
return;
animated = !!(flags & FlagT(ParticleTextureFlags::animated));
blendmode = BlendMode((flags & FlagT(ParticleTextureFlags::blend)) >> 1);
@@ -254,17 +250,6 @@ void ParticleParameters::serialize(std::ostream &os, u16 protocol_ver) const
texture.serialize(os, protocol_ver, true, true);
}
template <typename T, T (reader)(std::istream& is)>
inline bool streamEndsBeforeParam(T& val, std::istream& is)
{
// This is kinda awful
T tmp = reader(is);
if (is.eof())
return true;
val = tmp;
return false;
}
void ParticleParameters::deSerialize(std::istream &is, u16 protocol_ver)
{
pos = readV3F32(is);
@@ -280,14 +265,25 @@ void ParticleParameters::deSerialize(std::istream &is, u16 protocol_ver)
glow = readU8(is);
object_collision = readU8(is);
if (streamEndsBeforeParam<u16, readU16>(node.param0, is))
if (!canRead(is))
return;
// >= 5.3.0-dev
node.param0 = readU16(is);
node.param2 = readU8(is);
node_tile = readU8(is);
if (streamEndsBeforeParam<v3f, readV3F32>(drag, is))
if (!canRead(is))
return;
// >= 5.6.0-dev
drag = readV3F32(is);
jitter.deSerialize(is);
bounce.deSerialize(is);
if (!canRead(is))
return;
// >= 5.9.0-dev
texture.deSerialize(is, protocol_ver, true, true);
}