dotnet/wpf · error · ArgumentOutOfRangeException

Major and minor version number components cannot be…

Error message

Major and minor version number components cannot be negative.

What it means

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.

Solutions

  1. Validate the major value is >= 0 before constructing VersionPair
  2. Read version components as ushort and convert safely (unchecked((short)u) is still invalid if negative — check the raw value)
  3. Fix the source producing negative values (bit-mask the low 16 bits of header data)

Example fix

// before
var v = new VersionPair((short)header.Major, (short)header.Minor); // throws if negative
// after
if (header.Major < 0 || header.Minor < 0)
    throw new InvalidDataException("Bad version in header");
var v = new VersionPair((short)header.Major, (short)header.Minor);
Defensive patterns

Strategy: validation

Validate before calling

if (major < 0) throw new ArgumentOutOfRangeException(nameof(major), "Version major must be non-negative.");

Type guard

static bool IsValidVersionComponent(short v) => v >= 0;

Try / catch

try { var v = new VersionPair(major, minor); }
catch (ArgumentOutOfRangeException ex) { Log($"Invalid version component: {ex.ParamName}"); throw; }

Prevention

When it happens

Trigger: Calling new VersionPair(major, minor) with a negative major value, often from a signed Int16 that overflowed or came from an unvalidated header field.

Common situations: Reading version bytes from a file and casting them to Int16 without masking to unsigned; constructing version pairs from user input or computed values.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/551203e3b2602fcd. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/VersionPair.cs:45

    {
        //------------------------------------------------------
        //
        //  Internal Constructors
        //
        //------------------------------------------------------

        #region Constructors

        /// <summary>
        /// Constructor for VersionPair with given major and minor numbers.
        /// </summary>
        /// <param name="major">major part of version</param>
        /// <param name="minor">minor part of version</param>
        internal VersionPair(Int16 major, Int16 minor)
        {
            if (major < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(major),
                            SR.VersionNumberComponentNegative);
            }

            if (minor < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minor),
                            SR.VersionNumberComponentNegative);
            }

            _major = major;
            _minor = minor;
        }

        #endregion Constructors

        //------------------------------------------------------
        //
        //  Internal Properties

View on GitHub (pinned to 81131a70a4)