{"record":{"id":"6bfdfb79c53cbe38","repo":"dotnet/wpf","slug":"sr-setlengthnotsupported","errorCode":null,"errorMessage":"SR.SetLengthNotSupported","messagePattern":"SR\\.SetLengthNotSupported","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CFStream.cs","lineNumber":192,"sourceCode":"                throw new System.ComponentModel.InvalidEnumArgumentException(\"origin\", (int) origin, typeof(SeekOrigin));\n        }\n\n        _safeIStream.Seek( offset, translatedSeekOrigin, out seekPos );\n\n        return seekPos;\n    }\n\n    /// <summary>\n    /// See .NET Framework SDK under System.IO.Stream\n    /// </summary>\n    /// <param name=\"newLength\">New length</param>\n    public override void SetLength( long newLength )\n    {\n        CheckDisposedStatus();\n\n        if (!CanWrite)\n        {\n            throw new NotSupportedException(SR.SetLengthNotSupported);\n        }\n\n        if( 0 > newLength )\n        {\n            throw new ArgumentOutOfRangeException(nameof(newLength),\n                SR.StreamLengthNegative);\n        }\n        \n        _safeIStream.SetSize( newLength );\n\n        // updating the stream pointer if the stream has been truncated.\n        if (newLength < this.Position)\n            this.Position = newLength;\n    }\n\n    /// <summary>\n    /// See .NET Framework SDK under System.IO.Stream\n    /// </summary>","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CFStream.cs#L174-L210","documentation":"CFStream.SetLength throws NotSupportedException with SR.SetLengthNotSupported when the stream was opened without write access. Changing a compound-file stream's size requires write mode; on a read-only stream the operation is unsupported, matching Stream contract. The check happens after the disposed check and before length validation.","triggerScenarios":"Calling SetLength on a CFStream obtained from a compound file opened read-only (e.g. FileInfo.OpenRead, FileAccess.Read, or a packaging stream opened with read access).","commonSituations":"Opening an .docx/.xlsx OLE compound file read-only and then trying to resize an embedded stream; code paths that assume the stream is writable because they were tested against FileStream in read-write mode.","solutions":["Open the compound file with write access (FileAccess.ReadWrite) so CanWrite is true","Guard with if (stream.CanWrite) before calling SetLength","If the stream is meant to be read-only, avoid resizing; copy data to a writable MemoryStream instead","Catch NotSupportedException and fall back to a writable copy"],"exampleFix":"// before\nreadOnlyCfStream.SetLength(newSize);\n// after\nif (!readOnlyCfStream.CanWrite) {\n    var ms = new MemoryStream();\n    readOnlyCfStream.CopyTo(ms);\n    ms.SetLength(newSize);\n}","handlingStrategy":"validation","validationCode":"if (!stream.CanWrite) throw new InvalidOperationException(\"Stream is read-only; SetLength is not allowed.\");","typeGuard":"static bool CanResize(Stream s) => s.CanWrite;","tryCatchPattern":"try { stream.SetLength(newLength); } catch (NotSupportedException) { /* copy to writable stream and retry */ }","preventionTips":["Check CanWrite before any mutating stream operation","Open compound files with FileAccess.ReadWrite when resizing is needed","Design read-only paths to avoid SetLength entirely"],"tags":["not-supported","read-only","stream","io","wpf"],"backgroundTag":"unsupported-operation","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"}