stride3d/stride · error · ArgumentOutOfRangeException

Count cannot be less than zero

Error message

Count cannot be less than zero

What it means

Argument validation in Crc32.Update: the count parameter is negative, which cannot describe a number of bytes to process. The faulting input is the count argument; buffer/offset were already accepted.

Solutions

  1. Clamp or assert count >= 0 before calling Update
  2. Recompute the remaining-bytes expression, e.g. (int)Math.Min(chunkSize, stream.Length - stream.Position)
  3. If no bytes remain, skip the Update call entirely

Example fix

// before
crc.Update(buffer, 0, (int)(expected - read)); // can be negative
// after
int remaining = (int)(expected - read);
if (remaining > 0) crc.Update(buffer, 0, Math.Min(remaining, buffer.Length));
Defensive patterns

Strategy: validation

Validate before calling

int remaining = (int)(total - processed);
if (remaining < 0) throw new InvalidOperationException("Underflow in remaining-byte calculation");

Prevention

When it happens

Trigger: Calling Update(buffer, offset, count) with count computed from an expression that went negative (e.g. stream length subtraction, leftover = length - position underflow).

Common situations: Hashing a stream in chunks where remaining bytes calculation underflows, or passing -1 as a 'default' value.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/3f50228e89192adf. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.IO/System.IO.Compression.Zip/Crc32.cs:121

        /// <param name="buffer">
        /// The buffer which contains the data
        /// </param>
        /// <param name="offset">
        /// The offset in the buffer where the data starts
        /// </param>
        /// <param name="count">
        /// The number of data bytes to update the CRC with.
        /// </param>
        public void Update(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", "Count cannot be less than zero");
            }

            if (offset < 0 || offset + count > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }

            this.Value ^= CrcSeed;

            while (--count >= 0)
            {
                this.Value = Crc32Table[(this.Value ^ buffer[offset++]) & 0xFF] ^ (this.Value >> 8);
            }

            this.Value ^= CrcSeed;
        }

        #endregion

View on GitHub (pinned to 96fad776d2)