{"record":{"id":"27d2807713360a81","repo":"dotnet/wpf","slug":"string-format-is-not-valid","errorCode":null,"errorMessage":"String format is not valid.","messagePattern":"String format is not valid\\.","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/ContainerUtilities.cs","lineNumber":191,"sourceCode":"        {\n            checked\n            {\n                bytesRead = 0;\n\n                CheckAgainstNull(reader, \"reader\");\n\n                bytesRead = reader.ReadInt32();   // Length of the string in bytes\n\n                String inString = null;\n\n                if (bytesRead > 0)\n                {\n                    try\n                    {\n                        if (reader.BaseStream.Length < bytesRead / 2)\n                        {\n#if !PBTCOMPILER\n                            throw new FileFormatException(SR.InvalidStringFormat);\n#else\n                        throw new SerializationException(SR.InvalidStringFormat);\n#endif\n                        }\n                    }\n                    catch (NotSupportedException)\n                    {\n                        // if the stream does not support the Length operator, it will throw a NotSupportedException.\n                    }\n\n                    inString = new String(reader.ReadChars(bytesRead / 2));\n\n                    // Make sure the length of string read matches the length specified\n                    if (inString.Length != (bytesRead / 2))\n                    {\n#if !PBTCOMPILER\n                        throw new FileFormatException(SR.InvalidStringFormat);\n#else","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/ContainerUtilities.cs#L173-L209","documentation":"Inside ReadByteLengthPrefixedDWordPaddedUnicodeString, the library verifies the underlying stream is long enough to contain the declared character data (reader.BaseStream.Length < bytesRead/2). When the stream is too short the declared string length exceeds available bytes, indicating a corrupt or truncated record; the desktop build throws FileFormatException(SR.InvalidStringFormat). This is a length-consistency check, not a formatting problem in user input.","triggerScenarios":"Calling CompoundFileReference.Load / CompoundFileStorageReference.Load on a stream where a length-prefixed string header claims more bytes than remain in the stream.","commonSituations":"Truncated downloads or copies, binary data corrupted by text-mode transfer, wrong stream position (reading from the middle of a file), or non-compound-file data passed to a compound-file reader.","solutions":["Re-acquire the file and verify its size/checksum; the data is truncated or corrupt.","Ensure the reader is positioned at the correct record start before Load (reset Position or re-open the stream).","Confirm the file was transferred in binary mode and not altered by encoding conversion.","Catch FileFormatException around the Load call and report the document as unreadable rather than retrying the same bytes."],"exampleFix":"// before\nvar reference = CompoundFileReference.Load(reader); // throws if stream too short\n// after\nif (reader.BaseStream.Length - reader.BaseStream.Position < 4)\n    throw new InvalidDataException(\"Stream too short to contain a compound-file reference.\");\nvar reference = CompoundFileReference.Load(reader);","handlingStrategy":"try-catch","validationCode":"if (reader.BaseStream.Length - reader.BaseStream.Position < 4) throw new InvalidDataException(\"Stream too short for length-prefixed string.\");","typeGuard":"static bool HasEnoughBytes(Stream s, long needed) => s != null && s.CanSeek && (s.Length - s.Position) >= needed;","tryCatchPattern":"try { var r = CompoundFileReference.Load(reader); }\ncatch (FileFormatException ex) { throw new InvalidDataException(\"Compound file data is truncated or corrupt.\", ex); }","preventionTips":["Verify file size/checksum before parsing","Reset stream Position before Load calls","Avoid text-mode transfers of binary compound files","Validate input is a real compound file before parsing"],"tags":["file-corruption","truncated-data","packaging","wpf"],"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"}