{"record":{"id":"e4921a8a07874f13","repo":"dotnet/wpf","slug":"dataspace-map-entry-is-not-valid","errorCode":null,"errorMessage":"DataSpace map entry is not valid.","messagePattern":"DataSpace map entry is not valid\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/DataSpaceManager.cs","lineNumber":1079,"sourceCode":"                        if (entryLength < 0)\n                            throw new FileFormatException(SR.CorruptedData);\n\n                        totalBytesRead = 4; // entryLength\n                        // Read the container reference entry\n                        CompoundFileReference entryRef =\n                            CompoundFileReference.Load( dataSpaceMapReader, out bytesRead );\n                        checked { totalBytesRead += bytesRead; }\n\n                        // Read data space string and add to data space mapping table\n                        string label = CU.ReadByteLengthPrefixedDWordPaddedUnicodeString(dataSpaceMapReader, out bytesRead);\n                        checked { totalBytesRead += bytesRead; }\n\n                        _dataSpaceMap[entryRef] = label;\n\n                        // Verify entryLength against what was actually read:\n                        if (entryLength != totalBytesRead)\n                        {\n                            throw new IOException(SR.DataSpaceMapEntryInvalid);\n                        }\n                    }\n                }\n            }\n        }\n    }\n\n    /// <summary>\n    /// Write the data space mapping table to underlying storage.\n    /// </summary>\n    private void WriteDataSpaceMap()\n    {\n        ThrowIfIncorrectUpdaterVersion();\n\n        StorageInfo dataSpaceStorage =\n            new StorageInfo( _associatedStorage, DataSpaceStorageName );\n        StreamInfo dataSpaceMapStreamInfo =\n            new StreamInfo ( dataSpaceStorage, DataSpaceMapTableName );","sourceCodeStart":1061,"sourceCodeEnd":1097,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/DataSpaceManager.cs#L1061-L1097","documentation":"After each DataSpaceMap entry is parsed, the declared entryLength is compared against the number of bytes actually consumed (length field + container reference + label). A mismatch throws IOException with SR.DataSpaceMapEntryInvalid, because a well-formed writer must make the declared length match the real entry size exactly.","triggerScenarios":"Opening a package whose DataSpaceMap entry declares a byte length that differs from the actual parsed size — e.g. the entry's label or container reference was written with wrong encoding/size, the entry was hand-patched, or a buggy writer miscounted.","commonSituations":"Packages produced by third-party OPC writers that miscalculate entry lengths; files modified by tools that inserted/removed bytes without updating entryLength; Unicode label strings saved with wrong encoding length.","solutions":["Re-save the package with the reference implementation or a conformant writer so entry lengths are recomputed correctly.","Inspect the DataSpaceMap with a binary editor and fix entryLength values to match actual entry byte counts.","Validate each map entry (declared length == 4 + reference size + label byte count) before handing the file to the library.","Catch IOException during Package.Open and treat it as a malformed package; recover content via repair tooling."],"exampleFix":"// before\n// writer: entryLength = label.Length (chars) -> mismatch, IOException\n// after\n// writer: entryLength = 4 + referenceSize + Encoding.Unicode.GetByteCount(label)","handlingStrategy":"try-catch","validationCode":"// for each map entry: entryLength must equal 4 + referenceSize + labelByteCount before open","typeGuard":null,"tryCatchPattern":"try { package = Package.Open(path); }\ncatch (IOException e) when (e.Message == SR.DataSpaceMapEntryInvalid)\n{\n    throw new InvalidDataException(\"DataSpaceMap entry lengths do not match actual entry sizes.\", e);\n}","preventionTips":["Compute entryLength as 4 + reference size + UTF-16 byte count of the label when writing.","Re-save third-party packages with a reference writer before relying on them.","Add round-trip write/read tests for DataSpaceMap entries in tools you build."],"tags":["file-format","packaging","compound-file","parsing"],"backgroundTag":"invalid-argument-format","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"}