aboutsummaryrefslogtreecommitdiff
path: root/azalea-inventory/src
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2026-01-12 09:38:47 -1030
committermat <git@matdoes.dev>2026-01-13 01:08:53 +0500
commitefb36d5fc0fe56a98f5795c53dfa109887cd5aae (patch)
tree3674f0b53afca4b3f1d58fc0406bcbac552ab961 /azalea-inventory/src
parent2310666975d0f7e06f44efa6032c6838f2a33f0c (diff)
downloadazalea-drasl-efb36d5fc0fe56a98f5795c53dfa109887cd5aae.tar.xz
fix memory leaks in azalea-protocol
Diffstat (limited to 'azalea-inventory/src')
-rw-r--r--azalea-inventory/src/components/mod.rs3
-rw-r--r--azalea-inventory/src/slot.rs21
2 files changed, 16 insertions, 8 deletions
diff --git a/azalea-inventory/src/components/mod.rs b/azalea-inventory/src/components/mod.rs
index bc2a8f20..5e055592 100644
--- a/azalea-inventory/src/components/mod.rs
+++ b/azalea-inventory/src/components/mod.rs
@@ -128,7 +128,8 @@ macro_rules! define_data_components {
Ok(match kind {
$( DataComponentKind::$x => {
- Self { $x: ManuallyDrop::new($x::azalea_read(buf)?) }
+ let v = $x::azalea_read(buf)?;
+ Self { $x: ManuallyDrop::new(v) }
}, )*
})
}
diff --git a/azalea-inventory/src/slot.rs b/azalea-inventory/src/slot.rs
index 78f0cbf8..92dd40b1 100644
--- a/azalea-inventory/src/slot.rs
+++ b/azalea-inventory/src/slot.rs
@@ -359,7 +359,12 @@ impl DataComponentPatch {
kind: DataComponentKind,
value: Option<DataComponentUnion>,
) {
- self.components.insert(kind, value);
+ let existing = self.components.insert(kind, value);
+ if let Some(Some(mut existing)) = existing {
+ // SAFETY: we just got it from self.components, so it must already be the
+ // correct type
+ unsafe { existing.drop_as(kind) };
+ }
}
}
@@ -384,21 +389,23 @@ impl AzaleaRead for DataComponentPatch {
return Ok(DataComponentPatch::default());
}
- let mut components = IndexMap::new();
+ let mut components = DataComponentPatch::default();
+
for _ in 0..components_with_data_count {
let component_kind = DataComponentKind::azalea_read(buf)?;
let component_data = DataComponentUnion::azalea_read_as(component_kind, buf)?;
- components.insert(component_kind, Some(component_data));
+ // SAFETY: it must be of the correct type because we just read using
+ // azalea_read_as
+ unsafe { components.unchecked_insert_component(component_kind, Some(component_data)) };
}
for _ in 0..components_without_data_count {
let component_kind = DataComponentKind::azalea_read(buf)?;
- components.insert(component_kind, None);
+ // SAFETY: the value is None so the kind doesn't matter anyways
+ unsafe { components.unchecked_insert_component(component_kind, None) };
}
- Ok(DataComponentPatch {
- components: Box::new(components),
- })
+ Ok(components)
}
}