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.
This commit is contained in:
Isaac Freund 2020-06-26 12:46:18 +02:00
parent 08d630567f
commit dfb03fd0cb
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11
2 changed files with 8 additions and 2 deletions

View File

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

View File

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