{"record":{"id":"65c9bd0a91a0905d","repo":"dotnet/wpf","slug":"fileformatexception-sourceuri-e","errorCode":null,"errorMessage":"FileFormatException(SourceUri, e)","messagePattern":"FileFormatException\\(SourceUri, e\\)","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontFace/FontDriver.cs","lineNumber":144,"sourceCode":"                    _technology = FontTechnology.TrueTypeCollection;\n                    seekPosition += 4; // skip version\n                    _numFaces = ReadOpenTypeLong(seekPosition);\n                }\n                else if (typeTag == TrueTypeTags.OTTO)\n                {\n                    _technology = FontTechnology.PostscriptOpenType;\n                    _numFaces = 1;\n                }\n                else\n                {\n                    _technology = FontTechnology.TrueType;\n                    _numFaces = 1;\n                }\n            }\n            catch (ArgumentOutOfRangeException e)\n            {\n                // convert exceptions from CheckedPointer to FileFormatException\n                throw new FileFormatException(SourceUri, e);\n            }\n        }\n\n        internal void SetFace(int faceIndex)\n        {\n            if (_technology == FontTechnology.TrueTypeCollection)\n            {\n                ArgumentOutOfRangeException.ThrowIfNegative(faceIndex);\n                ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(faceIndex, _numFaces);\n            }\n            else\n            {\n                if (faceIndex != 0)\n                    throw new ArgumentOutOfRangeException(nameof(faceIndex), SR.FaceIndexValidOnlyForTTC);\n            }\n\n            try\n            {","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontFace/FontDriver.cs#L126-L162","documentation":"TrueTypeFontDriver's constructor reads the font file's offset table; CheckedPointer bounds-checks every access and throws ArgumentOutOfRangeException when a table offset/size points outside the file. The driver catches it and rethrows FileFormatException(SourceUri, e), converting 'bad pointer' into 'this font file is malformed or truncated'.","triggerScenarios":"Constructing a TrueTypeFontDriver (e.g. via ComputeTypefaceFontDriver / font enumeration) on a font stream whose header claims offsets or table lengths beyond the actual stream length.","commonSituations":"Corrupted font file from an interrupted download, font embedded in a resource that was truncated, disk-full during font deployment, running the parser on a non-font file renamed to .ttf.","solutions":["Re-install or re-download the font file and verify its size/checksum","Catch FileFormatException around font loading and fall back to a fallback font family","Check InnerException for the CheckedPointer offset that ran out of range","Validate the file is a real TrueType font (starts with 0x00010000 or 'true') before loading"],"exampleFix":"// before\ndriver = new TrueTypeFontDriver(stream, uri); // throws on truncated font\n// after\ntry { driver = new TrueTypeFontDriver(stream, uri); }\ncatch (FileFormatException e) {\n    log.Warn($\"Malformed font {uri}: {e.Message}\");\n    driver = null; // fall back\n}","handlingStrategy":"try-catch","validationCode":"using var fs = File.OpenRead(path);\nif (fs.Length < 12) throw new IOException(\"Not a TrueType font: too small\");\nSpan<byte> hdr = stackalloc byte[4]; fs.ReadExactly(hdr);\nbool plausible = hdr[0]==0 && hdr[1]==1 && hdr[2]==0 && hdr[3]==0; // 0x00010000","typeGuard":"bool LooksLikeTrueType(byte[] b) => b.Length >= 4 && (b[0]==0 && b[1]==1 && b[2]==0 && b[3]==0 || b[0]==(byte)'t' && b[1]==(byte)'r' && b[2]==(byte)'u' && b[3]==(byte)'e');","tryCatchPattern":"try { LoadFont(stream); }\ncatch (FileFormatException e)\n{\n    log.Warn($\"Font {e.SourceUri} is malformed: {e.InnerException?.Message}\");\n    UseFallbackFont();\n}","preventionTips":["Verify font checksums after download/deployment","Use binary-safe transfer for .ttf files","Isolate each font load in its own try-catch so one bad file doesn't abort enumeration"],"tags":["wpf","fonts","truetype","corrupt-file"],"backgroundTag":"file-read-failed","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}