{"record":{"id":"25936db13ca2332f","repo":"dotnet/wpf","slug":"fileformatexception-sourceuri","errorCode":null,"errorMessage":"FileFormatException(SourceUri)","messagePattern":"FileFormatException\\(SourceUri\\)","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontFace/FontDriver.cs","lineNumber":188,"sourceCode":"                    seekPosition += (4 + 4 + 4 * faceIndex);\n\n                    _directoryOffset = ReadOpenTypeLong(seekPosition);\n\n                    seekPosition = _fileStream + (_directoryOffset + 4);\n                    // 4 means that we skip the version number\n                }\n\n                _faceIndex = faceIndex;\n\n                int numTables = ReadOpenTypeUShort(seekPosition);\n                seekPosition += 2;\n\n                // quick check for malformed fonts, see if numTables is too large\n                // file size should be >= sizeof(offset table) + numTables * (sizeof(directory entry) + minimum table size (4))\n                long minimumFileSize = (4 + 2 + 2 + 2 + 2) + numTables * (4 + 4 + 4 + 4 + 4);\n                if (_fileStream.Size < minimumFileSize)\n                {\n                    throw new FileFormatException(SourceUri);\n                }\n\n                _tableDirectory = new DirectoryEntry[numTables];\n\n                // skip searchRange, entrySelector and rangeShift\n                seekPosition += 6;\n\n                // I can't use foreach here because C# disallows modifying the current value\n                for (int i = 0; i < _tableDirectory.Length; ++i)\n                {\n                    _tableDirectory[i].tag = (TrueTypeTags)ReadOpenTypeLong(seekPosition);\n                    seekPosition += 8; // skip checksum\n                    int offset = ReadOpenTypeLong(seekPosition);\n                    seekPosition += 4;\n                    int length = ReadOpenTypeLong(seekPosition);\n                    seekPosition += 4;\n\n                    _tableDirectory[i].pointer = _fileStream.CheckedProbe(offset, length);","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontFace/FontDriver.cs#L170-L206","documentation":"SetFace reads the sfnt offset table's numTables field and sanity-checks the file: its size must be at least the offset table plus numTables directory entries. When numTables is absurdly large relative to the file, the font is treated as malformed and FileFormatException(SourceUri) is thrown.","triggerScenarios":"Calling SetFace (or ComputeSubset) on a font whose binary header reports a numTables count inconsistent with the actual stream length — typical of corrupt, truncated, or counterfeit font files.","commonSituations":"Damaged .ttf/.ttc after a bad transfer, fuzzed/hostile font inputs, testing the font pipeline with random bytes, fonts stored inside archives that were extracted incompletely.","solutions":["Replace the font file with a verified copy and confirm its byte size","Catch FileFormatException around font loading and exclude the bad font","Sanity-check the font before loading (file size > 12 bytes and header plausible)","Scan deployment for partially-written font files"],"exampleFix":"// before\nvar fi = new FileInfo(path);\nif (fi.Length == 0) throw ...;\ndriver.SetFace(0);\n// after\nvar fi = new FileInfo(path);\nif (fi.Length < 12) throw new IOException($\"Font file too small: {path}\");\ndriver.SetFace(0);","handlingStrategy":"try-catch","validationCode":"long minSize = 12; // offset table only; full check needs numTables\nif (stream.Length < minSize) throw new IOException(\"Font file too small\");","typeGuard":null,"tryCatchPattern":"try { driver.SetFace(0); }\ncatch (FileFormatException e)\n{\n    log.Error($\"Malformed font header in {e.SourceUri}\");\n    ExcludeFont(path);\n}","preventionTips":["Validate font files with a font tool before shipping them","Detect partially-written files (compare to manifest sizes)","Treat fonts from untrusted sources as suspect and pre-validate"],"tags":["wpf","fonts","truetype","malformed-font"],"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-22T01:17:13.364Z"}