{"record":{"id":"d8104f3bace28017","repo":"dotnet/wpf","slug":"sr-seeknegative-compressemulationstream","errorCode":null,"errorMessage":"SR.SeekNegative","messagePattern":"SR\\.SeekNegative","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompressEmulationStream.cs","lineNumber":93,"sourceCode":"                case SeekOrigin.Current:\n                    {\n                        checked { temp = _tempStream.Position + offset; }\n                        break;\n                    }\n                case SeekOrigin.End:\n                    {\n                        checked { temp = _tempStream.Length + offset; }\n                        break;\n                    }\n                default:\n                    {\n                        throw new ArgumentOutOfRangeException(nameof(origin), SR.SeekOriginInvalid);\n                    }\n            }\n\n            if (temp < 0)\n            {\n                throw new ArgumentException(SR.SeekNegative);\n            }\n\n            return _tempStream.Seek(offset, origin);\n        }\n\n        /// <summary>\n        /// SetLength\n        /// </summary>\n        public override void SetLength(long newLength)\n        {\n            CheckDisposed();\n\n            _tempStream.SetLength(newLength);\n\n            // truncation always involves change of stream pointer\n            if (newLength < _tempStream.Position)\n                _tempStream.Position = newLength;\n","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompressEmulationStream.cs#L75-L111","documentation":"CompressEmulationStream.Seek throws ArgumentOutOfRangeException when the computed new position is negative. After resolving the offset relative to Begin/Current/End, a resulting position below zero is invalid for a stream and the seek is rejected.","triggerScenarios":"Seek(offset, SeekOrigin.Begin) or Current with a negative resulting position — e.g. Seek(-10, SeekOrigin.Begin) or a large negative offset relative to Current — invoked via Position or directly.","commonSituations":"Off-by-one buffer math, subtracting more than the bytes read from Current, rewinding before position 0 when parsing headers.","solutions":["Clamp or validate the computed offset so it is >= 0 before calling Seek.","Use SeekOrigin.Current only when you know the current position plus offset is non-negative.","Store positions with long and check before seeking."],"exampleFix":"// before\nstream.Seek(-(headerSize + extra), SeekOrigin.Current);\n// after\nlong target = stream.Position - (headerSize + extra);\nif (target < 0) target = 0;\nstream.Seek(target, SeekOrigin.Begin);","handlingStrategy":"validation","validationCode":"long ComputeSeekTarget(long current, long length, long offset, SeekOrigin origin)\n{\n    long target = origin switch\n    {\n        SeekOrigin.Begin => offset,\n        SeekOrigin.Current => current + offset,\n        SeekOrigin.End => length + offset,\n        _ => throw new ArgumentOutOfRangeException(nameof(origin))\n    };\n    if (target < 0) throw new ArgumentException(\"Seek target is negative\", nameof(offset));\n    return target;\n}","typeGuard":"bool IsValidSeek(long current, long length, long offset, SeekOrigin origin) =>\n    (origin == SeekOrigin.Begin ? offset :\n     origin == SeekOrigin.Current ? current + offset : length + offset) >= 0;","tryCatchPattern":"try { es.Seek(offset, SeekOrigin.Current); }\ncatch (ArgumentException) { es.Seek(0, SeekOrigin.Begin); }","preventionTips":["Compute targets in long and validate >= 0 before seeking","Clamp rewinds to position 0","Audit header-parsing rewind math for underflow"],"tags":["wpf","stream","seek","negative-offset","argument"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}