config: fix invalid memory access on theme loading

Fixes GHOSTTY-1X in Sentry

The comment explains.
This commit is contained in:
Mitchell Hashimoto
2024-11-22 09:44:23 -08:00
parent 49581ae8fc
commit e53eb9bd6f

View File

@ -2744,13 +2744,9 @@ fn loadTheme(self: *Config, theme: Theme) !void {
/// Call this once after you are done setting configuration. This /// Call this once after you are done setting configuration. This
/// is idempotent but will waste memory if called multiple times. /// is idempotent but will waste memory if called multiple times.
pub fn finalize(self: *Config) !void { pub fn finalize(self: *Config) !void {
const alloc = self._arena.?.allocator();
// We always load the theme first because it may set other fields // We always load the theme first because it may set other fields
// in our config. // in our config.
if (self.theme) |theme| { if (self.theme) |theme| {
try self.loadTheme(theme);
// If we have different light vs dark mode themes, disable // If we have different light vs dark mode themes, disable
// window-theme = auto since that breaks it. // window-theme = auto since that breaks it.
if (!std.mem.eql(u8, theme.light, theme.dark)) { if (!std.mem.eql(u8, theme.light, theme.dark)) {
@ -2758,8 +2754,14 @@ pub fn finalize(self: *Config) !void {
// because it'll force the theme based on the Ghostty theme. // because it'll force the theme based on the Ghostty theme.
if (self.@"window-theme" == .auto) self.@"window-theme" = .system; if (self.@"window-theme" == .auto) self.@"window-theme" = .system;
} }
// Warning: loadTheme will deinit our existing config and replace
// it so all memory from self prior to this point will be freed.
try self.loadTheme(theme);
} }
const alloc = self._arena.?.allocator();
// If we have a font-family set and don't set the others, default // If we have a font-family set and don't set the others, default
// the others to the font family. This way, if someone does // the others to the font family. This way, if someone does
// --font-family=foo, then we try to get the stylized versions of // --font-family=foo, then we try to get the stylized versions of