Skip to content

ZipArchiveEntry.Open expectedly throws an ArgumentOutOfRangeException #127834

Closed

Description

@rjacobw

Describe the bug

ZipArchiveEntry.cs incorrectly assumes that constructing a MemoryStream with a negative length is "okay" (see ZipArchiveEntry.GetCompressedData) The reality is that the MemoryStream constructor now throws an ArgumentOutOfRangeException (here) when an unchecked cast causes an uint to wrap around.

This appears to be new to netcore (versus netframework), and does cause unexpected failures where none had previously existed.

To Reproduce

Call ZipArchiveEntry.Open on a very large entry (> int.MaxValue).

Exceptions (if any)

ArgumentOutOfRangeException is thrown from the MemoryStream constructor.

Further technical details

This is no longer true -

<ZipArchiveEntry.cs>

// if _uncompressedSize > int.MaxValue, it's still okay, because MemoryStream will just
// grow as data is copied into it
_storedUncompressedData = new MemoryStream((int)_uncompressedSize);
<MemoryStream.cs>

public MemoryStream(int capacity)
{
    ArgumentOutOfRangeException.ThrowIfNegative(capacity);
    ArgumentOutOfRangeException.ThrowIfGreaterThan(capacity, MemStreamMaxLength);

Activity

  1. added
    untriaged
    on May 5, 2026
  2. transferred this issue fromdotnet/sdkon May 5, 2026
  3. added
    untriaged
    on May 5, 2026
  4. dotnet-policy-service commented on May 5, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @karelz, @dotnet/area-system-io-compression
    See info in area-owners.md if you want to be subscribed.

  5. KalleOlaviNiemitalo commented on May 6, 2026

    @KalleOlaviNiemitalo

    Should it be casting (uint)_uncompressedSize instead?

    … no, the MemoryStream constructor takes int capacity rather than long capacity.

    private static int MemStreamMaxLength => Array.MaxLength;

    public MemoryStream(int capacity)
    {
    ArgumentOutOfRangeException.ThrowIfNegative(capacity);
    ArgumentOutOfRangeException.ThrowIfGreaterThan(capacity, MemStreamMaxLength);
    _buffer = capacity != 0 ? new byte[capacity] : [];

    So MemoryStream just doesn't support streams larger than what byte[] can hold. If the uncompressed size of the zip entry actually is larger than that, then I suppose some exception needs to be thrown, but perhaps not ArgumentOutOfRangeException.

  6. rjacobw commented on May 6, 2026

    @rjacobw
    Author

    Could you not use a segmented stream approach so as not to break the Zip implementation for archives containing large entries? It seems like ZipArchiveEntry already allows for large entries (here), but now that MemoryStream throws an exception from the constructor, ZipArchiveEntry can no longer use the multiple buffer approach mentioned in IsOpenableFinalVerifications.

  7. KalleOlaviNiemitalo commented on May 6, 2026

    @KalleOlaviNiemitalo

    Multiple buffers were implemented in dotnet/corefx@0920c09 but AFAICT only for compressed data, not uncompressed. The commit message mentions:

    There are other limitations working with large files in archives that are not addressed, including opening a ZipArchiveEntry with a greater than 2GB uncompressed size.

    so I'm not sure whether such huge entries can ever have been opened in update mode.

  8. removed
    untriaged
    on May 7, 2026
  9. alinpahontu2912 commented on May 7, 2026

    @alinpahontu2912
    Member

    Hey @rjacobw, how are you using the archive? If you are opening the archive in update mode, it fails because we don't support updating large entries in memory. We are relying on memory stream, which can only grow up to array.maxlength, not int.maxvalue.

  10. rjacobw commented on May 7, 2026

    @rjacobw
    Author

    @alinpahontu2912 we do open archives in update mode, and have been since NetFramework. We've recently gone through a very large push to build and ship on top of net10 (moving off net48), and we're now seeing an error where we had never seen any such errors before.

    After digging a bit, I'm not sure how it ever worked (but I'm sure it did). Even looking back at MemoryStream on ReferenceSource would suggest that the comment in ZipArchiveEntry indicating that MemoryStream can handle negative capacities was wrong - though I'd presume that comment was written at time when perhaps MemoryStream did happy accept negative capacities.

    Given that MemoryStream used to limit capacities to int.MaxValue (here), but now uses Array.MaxLength (a smaller value), I wonder if perhaps we're seeing entries fail to open in update mode simply because the sizes are in that gap between those two values.

  11. alinpahontu2912 commented on May 7, 2026

    @alinpahontu2912
    Member

    @rjacobw you're right that the gap between Array.MaxLength (0x7FFFFFC7) and int.MaxValue is real, but it's only 56 bytes. Even in .NET Framework, allocating a byte[] larger than Array.MaxLength would fail with OutOfMemoryException. So entries in that narrow gap would not have succeeded before either. The failure mode just changed from OutOfMemoryException to ArgumentOutOfRangeException.

    The more likely scenario for entries > int.MaxValue is the unchecked (int) cast wrapping to a negative value. We'll look into addressing this.

  12. added this to the Future milestone on Jun 8, 2026
  13. added theissue type on Jun 10, 2026
  14. alinpahontu2912 commented on Jul 8, 2026

    @alinpahontu2912
    Member

    I believe this is fixed by #128319

  15. locked and limited conversation to collaborators on Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions