{"record":{"id":"551203e3b2602fcd","repo":"dotnet/wpf","slug":"major-and-minor-version-number-components-cannot-be-negative","errorCode":null,"errorMessage":"Major and minor version number components cannot be negative.","messagePattern":"Major and minor version number components cannot be negative\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/VersionPair.cs","lineNumber":45,"sourceCode":"    {\n        //------------------------------------------------------\n        //\n        //  Internal Constructors\n        //\n        //------------------------------------------------------\n\n        #region Constructors\n\n        /// <summary>\n        /// Constructor for VersionPair with given major and minor numbers.\n        /// </summary>\n        /// <param name=\"major\">major part of version</param>\n        /// <param name=\"minor\">minor part of version</param>\n        internal VersionPair(Int16 major, Int16 minor)\n        {\n            if (major < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(major),\n                            SR.VersionNumberComponentNegative);\n            }\n\n            if (minor < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(minor),\n                            SR.VersionNumberComponentNegative);\n            }\n\n            _major = major;\n            _minor = minor;\n        }\n\n        #endregion Constructors\n\n        //------------------------------------------------------\n        //\n        //  Internal Properties","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/VersionPair.cs#L27-L63","documentation":"The VersionPair constructor validates its Int16 major component and throws ArgumentOutOfRangeException if it is negative. Version components of structured storage headers are unsigned in the format spec, so negative values indicate caller error.","triggerScenarios":"Calling new VersionPair(major, minor) with a negative major value, often from a signed Int16 that overflowed or came from an unvalidated header field.","commonSituations":"Reading version bytes from a file and casting them to Int16 without masking to unsigned; constructing version pairs from user input or computed values.","solutions":["Validate the major value is >= 0 before constructing VersionPair","Read version components as ushort and convert safely (unchecked((short)u) is still invalid if negative — check the raw value)","Fix the source producing negative values (bit-mask the low 16 bits of header data)"],"exampleFix":"// before\nvar v = new VersionPair((short)header.Major, (short)header.Minor); // throws if negative\n// after\nif (header.Major < 0 || header.Minor < 0)\n    throw new InvalidDataException(\"Bad version in header\");\nvar v = new VersionPair((short)header.Major, (short)header.Minor);","handlingStrategy":"validation","validationCode":"if (major < 0) throw new ArgumentOutOfRangeException(nameof(major), \"Version major must be non-negative.\");","typeGuard":"static bool IsValidVersionComponent(short v) => v >= 0;","tryCatchPattern":"try { var v = new VersionPair(major, minor); }\ncatch (ArgumentOutOfRangeException ex) { Log($\"Invalid version component: {ex.ParamName}\"); throw; }","preventionTips":["Validate header-derived values before constructing VersionPair","Parse version fields as unsigned 16-bit values","Fuzz-test parsers against corrupt header data"],"tags":["argument-out-of-range","validation","version"],"backgroundTag":"argument-out-of-range","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"}