Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
Refuse .fon glyphs too wide for TrueType before decoding them
SeoJCalive9986e341134d
diff --git a/src/Filee.Engines/Fonts/FonOutlineBuilder.cs b/src/Filee.Engines/Fonts/FonOutlineBuilder.csindex 18a55c3b..726b880b 100644--- a/src/Filee.Engines/Fonts/FonOutlineBuilder.cs+++ b/src/Filee.Engines/Fonts/FonOutlineBuilder.cs@@ -11,6 +11,9 @@ internal static class FonOutlineBuilderprivate readonly record struct Corner(int X, int Y);private readonly record struct Edge(Corner From, Corner To);+ /// <summary>Scales the strike's pixel height into TrueType font units.</summary>+ internal static int Scale(int height) => height <= 1024 ? 1024 / height : 1;+public static SfntFont Build(FonStrike strike, int resourceCount = 1, int sameHeightOrdinal = 1) =>Build(strike, CancellationToken.None, resourceCount, sameHeightOrdinal);@@ -24,7 +27,7 @@ internal static class FonOutlineBuilderif (resourceCount < 1 || sameHeightOrdinal < 1 || strike.Glyphs.Count is < 1 or > 65534)throw new InvalidDataException("FON resource or glyph count exceeds TrueType limits.");var height = strike.PixelHeight;- var scale = height <= 1024 ? 1024 / height : 1;+ var scale = Scale(height);var upem = checked(height * scale);if (upem is < 16 or > 16384)throw new InvalidDataException("FON pixel height exceeds TrueType units-per-em limits.");diff --git a/src/Filee.Engines/Fonts/FonReader.cs b/src/Filee.Engines/Fonts/FonReader.csindex 987bb3d8..1bc65706 100644--- a/src/Filee.Engines/Fonts/FonReader.cs+++ b/src/Filee.Engines/Fonts/FonReader.cs@@ -162,6 +162,7 @@ internal static class FonReadervar bitsOffset = (long)U32(fnt, 0x71);if (bitsOffset < tableEnd || bitsOffset > size)throw new InvalidDataException("FNT bitmap region offset is outside the resource.");+ var scale = FonOutlineBuilder.Scale(height);var glyphs = decode ? new List<FonGlyph>(count) : null;for (var i = 0; i < count; i++){@@ -170,6 +171,8 @@ internal static class FonReadervar width = U16(fnt, entry);var offset = version == 0x0200 ? U16(fnt, entry + 2) : U32(fnt, entry + 2);var bytes = ((long)width + 7) / 8 * height;+ if ((long)width * scale > ushort.MaxValue)+ throw new InvalidDataException($"FON glyph advance exceeds the {ushort.MaxValue}-unit TrueType limit.");if (offset < bitsOffset || !Fits(size, offset, bytes))throw new InvalidDataException($"FNT bitmap for character {first + i} is outside the resource.");if (!decode)diff --git a/tests/Filee.Engines.Tests/FonTests.cs b/tests/Filee.Engines.Tests/FonTests.csindex 638a02ae..f0e434cf 100644--- a/tests/Filee.Engines.Tests/FonTests.cs+++ b/tests/Filee.Engines.Tests/FonTests.cs@@ -867,18 +867,57 @@ public class FonTests(EngineFixture fx) : IClassFixture<EngineFixture>[Fact]public async Task Aliased_glyphs_over_filewide_pixel_limit_fail_without_outputs(){- var data = Container(AliasedFnt(2048, 8), AliasedFnt(2049, 8));+ var data = Container(AliasedFnt(16, 1024), AliasedFnt(17, 1024));var error = Assert.Throws<InvalidDataException>(() => FonReader.Read(data, TestContext.Current.CancellationToken));Assert.Contains("unit limit", error.Message);Assert.Contains($"{FonReader.MaxGlyphPixels:N0}", error.Message);- await AssertNothingWrittenAsync(data, ["Name-8px.ttf", "Name-8px-2.ttf"], FileState.Failed, "unit limit");+ await AssertNothingWrittenAsync(data, ["Name-1024px.ttf", "Name-1024px-2.ttf"], FileState.Failed, "unit limit");+ }++ [Fact]+ public void Glyph_advance_above_65535_is_rejected_before_decoding()+ {+ var data = FonBuilder.Container([AliasedFnt(64, 1, 1)]);++ var error = Assert.Throws<InvalidDataException>(() => FonReader.Read(data, TestContext.Current.CancellationToken));+ Assert.Contains("65535", error.Message);+ }++ [Fact]+ public void Blank_glyph_with_advance_above_signed_coordinate_limit_converts()+ {+ var resource = AliasedFnt(33, 1, 2);+ BinaryPrimitives.WriteUInt16LittleEndian(resource.AsSpan(0x76 + 4), 0);+ var data = FonBuilder.Container([resource]);+ var strike = Assert.Single(FonReader.Read(data, TestContext.Current.CancellationToken));+ var font = FontFile.Load(FontFile.Save(+ FonOutlineBuilder.Build(strike, TestContext.Current.CancellationToken), "ttf", TestContext.Current.CancellationToken),+ TestContext.Current.CancellationToken);++ Assert.Equal(33 * 1024, Advance(font, 1));+ Assert.Null(GlyfTable.ReadAll(font)[1]);+ }++ [Fact]+ public void Shared_large_bitmap_is_refused_with_less_than_one_megabyte_allocated()+ {+ var data = FonBuilder.Container([AliasedFnt(4096, 1024, 224)], alignmentShift: 9);+ Assert.Single(FonReader.Read(FonBuilder.V2Fixed(), TestContext.Current.CancellationToken));++ var before = GC.GetAllocatedBytesForCurrentThread();+ var error = Assert.Throws<InvalidDataException>(() => FonReader.Read(data, TestContext.Current.CancellationToken));+ var allocated = GC.GetAllocatedBytesForCurrentThread() - before;++ Assert.Contains("unit limit", error.Message);+ Assert.Contains($"{FonReader.MaxGlyphPixels:N0}", error.Message);+ Assert.True(allocated < 1_000_000, $"Reader allocated {allocated:N0} bytes.");}[Fact]public void Aliased_glyphs_at_filewide_pixel_limit_are_accepted(){- var data = Container(AliasedFnt(2048, 8), AliasedFnt(2048, 8));+ var data = Container(AliasedFnt(16, 1024), AliasedFnt(16, 1024));var strikes = FonReader.Read(data, TestContext.Current.CancellationToken);Assert.Equal(FonReader.MaxGlyphPixels, strikes.Sum(s => s.Glyphs.Sum(g => (long)g.Width * s.PixelHeight)));Assert.All(strikes, s => Assert.All(s.Glyphs, g => Assert.False(g.Pixels[0, 0])));