From 813f11a5ebc39a29ddb8dfae4228f2cc2ae7cb22 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 19 Aug 2022 22:25:11 -0700 Subject: [PATCH] fix crash that could happen looking up fallback --- src/font/FallbackSet.zig | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/font/FallbackSet.zig b/src/font/FallbackSet.zig index fabff2adc..57faf1fee 100644 --- a/src/font/FallbackSet.zig +++ b/src/font/FallbackSet.zig @@ -71,7 +71,7 @@ pub fn getOrAddGlyph( errdefer _ = self.glyphs.remove(glyphKey); // Go through each familiy and look for a matching glyph - var fam_i: usize = 0; + var fam_i: ?usize = 0; const glyph = glyph: { for (self.families.items) |*family, i| { fam_i = i; @@ -97,13 +97,19 @@ pub fn getOrAddGlyph( // If we are regular, we use a fallback character log.warn("glyph not found, using fallback. codepoint={x}", .{utf32}); - fam_i = 0; + fam_i = null; break :glyph try self.families.items[0].addGlyph(alloc, ' ', style); }; - gop.value_ptr.* = fam_i; + // If we found a real value, then cache it. + // TODO: support caching fallbacks too + if (fam_i) |i| + gop.value_ptr.* = i + else + _ = self.glyphs.remove(glyphKey); + return GetOrAdd{ - .family = fam_i, + .family = fam_i orelse 0, .glyph = glyph, // Technically possible that we found this in a cache...