From dfb03fd0cb28059211432513fd6a94679feb9d6b Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Fri, 26 Jun 2020 12:46:18 +0200 Subject: [PATCH] command: dupe mode name memory The hashmap doesn't take ownership of the memory pointer to by these slices, so we need to dupe them to avoid use-after-free. --- river/Config.zig | 3 +++ river/command/declare_mode.zig | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/river/Config.zig b/river/Config.zig index 015d02d..ea45f38 100644 --- a/river/Config.zig +++ b/river/Config.zig @@ -61,12 +61,15 @@ pub fn init(self: *Self) !void { self.outer_padding = 8; self.mode_to_id = std.StringHashMap(usize).init(util.gpa); + errdefer self.mode_to_id.deinit(); try self.mode_to_id.putNoClobber("normal", 0); self.modes = std.ArrayList(std.ArrayList(Mapping)).init(util.gpa); + errdefer self.modes.deinit(); try self.modes.append(std.ArrayList(Mapping).init(util.gpa)); self.float_filter = std.ArrayList([*:0]const u8).init(util.gpa); + errdefer self.float_filter.deinit(); // Float views with app_id "float" try self.float_filter.append("float"); diff --git a/river/command/declare_mode.zig b/river/command/declare_mode.zig index 943ede8..06472a9 100644 --- a/river/command/declare_mode.zig +++ b/river/command/declare_mode.zig @@ -18,6 +18,7 @@ const std = @import("std"); const c = @import("../c.zig"); +const util = @import("../util.zig"); const Error = @import("../command.zig").Error; const Mapping = @import("../Mapping.zig"); @@ -45,7 +46,9 @@ pub fn declareMode( return Error.CommandFailed; } - try config.mode_to_id.putNoClobber(new_mode_name, config.modes.items.len); - errdefer _ = config.mode_to_id.remove(new_mode_name); + const owned_name = try std.mem.dupe(util.gpa, u8, new_mode_name); + errdefer util.gpa.free(owned_name); + try config.mode_to_id.putNoClobber(owned_name, config.modes.items.len); + errdefer _ = config.mode_to_id.remove(owned_name); try config.modes.append(std.ArrayList(Mapping).init(allocator)); }