{"record":{"id":"b8da2b9995550998","repo":"dotnet/wpf","slug":"sr-cfrcorrupt","errorCode":null,"errorMessage":"SR.CFRCorrupt","messagePattern":"SR\\.CFRCorrupt","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CompoundFileReference.cs","lineNumber":168,"sourceCode":"        /// <remarks>\n        /// Side effect of change the stream pointer\n        /// </remarks>\n        /// <exception cref=\"FileFormatException\">Throws a FileFormatException if any formatting errors are encountered</exception>\n        internal static CompoundFileReference Load(BinaryReader reader, out int bytesRead)\n        {\n            ContainerUtilities.CheckAgainstNull( reader, \"reader\" );\n\n            bytesRead = 0;  // running count of how much we've read - sanity check\n\n            // create the TypeMap\n            // reconstitute ourselves from the given BinaryReader\n            \n            // in this version, the next Int32 is the number of entries\n            Int32 entryCount = reader.ReadInt32();\n            bytesRead += ContainerUtilities.Int32Size;\n            // EntryCount of zero indicates the root storage.\n            if (entryCount < 0)\n                throw new FileFormatException(\n                    SR.CFRCorrupt);\n\n            // need a temp collection because we don't know what we're dealing with until a non-storage component\n            // type is encountered\n            StringCollection storageList = null;\n            String streamName = null;\n\n            // loop through the entries - accumulating strings until we know what kind of object\n            // we ultimately need\n            int byteLength;     // reusable\n            while (entryCount > 0)\n            {\n                // first Int32 tells us what kind of component this entry represents\n                RefComponentType refType = (RefComponentType)reader.ReadInt32();\n                bytesRead += ContainerUtilities.Int32Size;\n\n                switch (refType)\n                {","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CompoundFileReference.cs#L150-L186","documentation":"CompoundFileReference.Load reads an Int32 entry count from the serialized reference; a negative count is impossible in a well-formed reference, so Load throws FileFormatException(SR.CFRCorrupt). This guards the reader against corrupt or maliciously crafted compound files whose reference records claim invalid entry counts.","triggerScenarios":"Load deserializes a reference where the Int32 at the entry-count position is negative — bytes shifted by an earlier mis-parse, file truncation landing the reader mid-data, or a crafted file with a negative count.","commonSituations":"Opening damaged compound documents (disk corruption, bad downloads); files produced by writers with mismatched reference serialization format; security fuzzing of OPC packages.","solutions":["Re-obtain the file from a trusted source; the reference record is corrupt.","Locate the first parse error — negative counts usually mean the reader is misaligned from an earlier bad read.","If you write these files, verify your reference serialization writes a non-negative entry count.","Run a compound-file validator/repair tool to pinpoint corruption."],"exampleFix":"// consumer-side guard before opening untrusted files\nif (!CompoundFileLooksPlausible(stream))\n    throw new InvalidDataException(\"compound file reference section is malformed\");\nvar cf = CompoundFile.Open(stream);","handlingStrategy":"validation","validationCode":"bool EntryCountLooksValid(byte[] record) { int c = BitConverter.ToInt32(record, EntryCountOffset); return c >= 0 && c < 4096; }","typeGuard":null,"tryCatchPattern":"try { var r = CompoundFileReference.Load(reader); } catch (FileFormatException) { QuarantineFile(sourcePath); }","preventionTips":["Validate compound files before opening untrusted documents","Quarantine files that fail structural validation","Only consume packages from conformant writers"],"tags":["file-format","corruption","deserialization","wpf"],"backgroundTag":"schema-validation-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"}