{"record":{"id":"62dea8859a860608","repo":"dotnet/wpf","slug":"sr-colorcontext-filetoolarge","errorCode":null,"errorMessage":"SR.ColorContext_FileTooLarge","messagePattern":"SR\\.ColorContext_FileTooLarge","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ColorContext.cs","lineNumber":625,"sourceCode":"\n                    using (FactoryMaker factoryMaker = new FactoryMaker())\n                    {\n                        HRESULT.Check(UnsafeNativeMethodsMilCoreApi.WICCodec.CreateColorContext(factoryMaker.ImagingFactoryPtr, out _colorContextHandle));\n                        HRESULT.Check(IWICCC.InitializeFromMemory(_colorContextHandle, rawBytes, (uint)numBytesRead));\n                    }\n\n                    return;\n                }\n                else\n                {\n                    bufferSize += _bufferSizeIncrement;\n                    byte[] newRawBytes = new byte[bufferSize];\n                    rawBytes.CopyTo(newRawBytes, 0);\n                    rawBytes = newRawBytes;\n                }\n            }\n\n            throw new ArgumentException(SR.ColorContext_FileTooLarge, filename);\n        }\n\n        /// Note: often the data buffer is larger than the actual data in it.\n        ///\n        /// dontThrowException is for preserving the 3.* behavior of ColorContext(SafeMILHandle)\n        ///\n        private void FromRawBytes(byte[] data, int dataLength, bool dontThrowException) \n        {\n            Invariant.Assert(dataLength <= data.Length);\n            Invariant.Assert(dataLength >= 0);\n            \n            UnsafeNativeMethods.PROFILEHEADER header;\n            UnsafeNativeMethods.PROFILE profile;\n\n            unsafe\n            {\n                fixed (void *dataPtr = data)\n                {","sourceCodeStart":607,"sourceCodeEnd":643,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ColorContext.cs#L607-L643","documentation":"ColorContext.FromStream throws ArgumentException(SR.ColorContext_FileTooLarge) when the profile stream is larger than the maximum supported size for an in-memory raw profile buffer. The reader accumulates chunks into rawBytes and, if it still needs more space after the size cap, gives up and throws with the filename as parameter.","triggerScenarios":"Calling ColorContext(Stream) / FromStream with a stream whose ICC profile data exceeds the library's maximum buffer size (a corrupted or maliciously oversized profile).","commonSituations":"Corrupt or truncated-then-padded ICC files; hostile XPS payloads containing huge profiles; passing a stream of the wrong file type by accident.","solutions":["Check the profile stream length before constructing ColorContext and reject oversized files","Validate the ICC file is well-formed (128-byte header, sane tag table) before loading","Regenerate or re-export the color profile from a profile editor","Catch ArgumentException and fall back to a standard profile"],"exampleFix":"// before\nvar ctx = new ColorContext(profileStream);\n// after\nif (profileStream.Length <= 4 * 1024 * 1024) { var ctx = new ColorContext(profileStream); } else { throw new InvalidOperationException(\"Profile too large\"); }","handlingStrategy":"validation","validationCode":"if (profileStream != null && profileStream.CanSeek && profileStream.Length > 4 * 1024 * 1024) throw new ArgumentException(\"ICC profile exceeds maximum supported size\");","typeGuard":null,"tryCatchPattern":"try { var ctx = new ColorContext(stream); } catch (ArgumentException ex) { /* reject oversized profile, use default profile */ }","preventionTips":["Check stream length before loading","Validate ICC header before trusting third-party profiles","Sanitize XPS inputs from untrusted sources"],"tags":["wpf","color-context","file-size"],"backgroundTag":"file-size-limit-exceeded","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"}