Fix use after free when dropping tools.

should fix #771
This commit is contained in:
IntegratedQuantum 2024-11-10 13:15:39 +01:00
parent f8a09ae89f
commit 28b0a51f77
2 changed files with 47 additions and 3 deletions

View File

@ -930,7 +930,7 @@ pub const Command = struct { // MARK: Command
if(_items[0].item != null) {
if(side == .server) {
const direction = vec.rotateZ(vec.rotateX(Vec3f{0, 1, 0}, -user.?.player.rot[0]), -user.?.player.rot[2]);
main.server.world.?.drop(_items[0], user.?.player.pos, direction, 20);
main.server.world.?.drop(_items[0].clone(), user.?.player.pos, direction, 20);
}
}
return;
@ -940,7 +940,7 @@ pub const Command = struct { // MARK: Command
}
if(side == .server) {
const direction = vec.rotateZ(vec.rotateX(Vec3f{0, 1, 0}, -user.?.player.rot[0]), -user.?.player.rot[2]);
main.server.world.?.drop(self.source.ref().*, user.?.player.pos, direction, 20);
main.server.world.?.drop(self.source.ref().clone(), user.?.player.pos, direction, 20);
}
cmd.executeBaseOperation(allocator, .{.delete = .{
.source = self.source,
@ -1053,7 +1053,7 @@ pub const Command = struct { // MARK: Command
}
if(side == .server) {
const direction = vec.rotateZ(vec.rotateX(Vec3f{0, 1, 0}, -user.?.player.rot[0]), -user.?.player.rot[2]);
main.server.world.?.drop(sourceStack.*, user.?.player.pos, direction, 20);
main.server.world.?.drop(sourceStack.clone(), user.?.player.pos, direction, 20);
}
cmd.executeBaseOperation(allocator, .{.delete = .{
.source = .{.inv = self.source, .slot = @intCast(sourceSlot)},

View File

@ -925,6 +925,33 @@ pub const Tool = struct { // MARK: Tool
main.globalAllocator.destroy(self);
}
pub fn clone(self: *const Tool) *Tool {
const result = main.globalAllocator.create(Tool);
result.* = .{
.craftingGrid = self.craftingGrid,
.materialGrid = self.materialGrid,
.tooltip = null,
.image = graphics.Image.init(main.globalAllocator, self.image.width, self.image.height),
.texture = null,
.seed = self.seed,
.pickaxePower = self.pickaxePower,
.axePower = self.axePower,
.shovelPower = self.shovelPower,
.damage = self.damage,
.durability = self.durability,
.maxDurability = self.maxDurability,
.swingTime = self.swingTime,
.mass = self.mass,
.handlePosition = self.handlePosition,
.inertiaHandle = self.inertiaHandle,
.centerOfMass = self.centerOfMass,
.inertiaCenterOfMass = self.inertiaCenterOfMass,
};
@memcpy(result.image.imageData, self.image.imageData);
return result;
}
pub fn initFromCraftingGrid(craftingGrid: [25]?*const BaseItem, seed: u32) *Tool {
const self = init();
self.seed = seed;
@ -1047,6 +1074,15 @@ pub const Item = union(enum) { // MARK: Item
}
}
pub fn clone(self: Item) Item {
switch(self) {
.baseItem => return self,
.tool => |tool| {
return .{.tool = tool.clone()};
}
}
}
pub fn stackSize(self: Item) u16 {
switch(self) {
.baseItem => |_baseItem| {
@ -1128,6 +1164,14 @@ pub const ItemStack = struct { // MARK: ItemStack
}
}
pub fn clone(self: *const ItemStack) ItemStack {
const item = self.item orelse return .{};
return .{
.item = item.clone(),
.amount = self.amount,
};
}
pub fn empty(self: *const ItemStack) bool {
return self.amount == 0;
}