From 13d9c0159d1aa3b194152a9f6b8f01a10835e347 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 11 Apr 2024 13:01:55 -0400 Subject: [PATCH] font: SharedGridSet.Key needs to clone the DerivedConfig Key contains pointers into DerivedConfig. Each surface has its own DerivedConfig. This would cause memory corruption if the surface that opened the initial font grid deallocates. Instead, let's make sure Key owns its own DerivedConfig. --- src/font/SharedGridSet.zig | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/font/SharedGridSet.zig b/src/font/SharedGridSet.zig index 561843bbd..d9df8f309 100644 --- a/src/font/SharedGridSet.zig +++ b/src/font/SharedGridSet.zig @@ -367,7 +367,9 @@ pub const DerivedConfig = struct { @"adjust-strikethrough-position": ?Metrics.Modifier, @"adjust-strikethrough-thickness": ?Metrics.Modifier, - pub fn init(alloc_gpa: Allocator, config: *const Config) !DerivedConfig { + /// Initialize a DerivedConfig. The config should be either a + /// config.Config or another DerivedConfig to clone from. + pub fn init(alloc_gpa: Allocator, config: anytype) !DerivedConfig { var arena = ArenaAllocator.init(alloc_gpa); errdefer arena.deinit(); const alloc = arena.allocator(); @@ -447,13 +449,20 @@ pub const Key = struct { pub fn init( alloc_gpa: Allocator, - config: *const DerivedConfig, + config_src: *const DerivedConfig, font_size: DesiredSize, ) !Key { var arena = ArenaAllocator.init(alloc_gpa); errdefer arena.deinit(); const alloc = arena.allocator(); + // Clone our configuration. We need to do this because the lifetime + // of the derived config is usually shorter than that of a key + // and we use pointers into the derived config for the key. We + // can remove this if we wanted by dupe-ing the memory we use + // from DerivedConfig below. + var config = try DerivedConfig.init(alloc, config_src); + var descriptors = std.ArrayList(discovery.Descriptor).init(alloc); defer descriptors.deinit(); for (config.@"font-family".list.items) |family| {