Skip to content

Image.Identify throws ArgumentOutOfRangeException on empty unseekable stream #1903

Description

@carlreinke

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • I have searched open and closed issues to ensure it has not already been reported

Description

Image.Identify throws ArgumentOutOfRangeException when provided an empty unseekable stream.

Stack trace:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: value
   at SixLabors.ImageSharp.IO.ChunkedMemoryStream.ThrowArgumentOutOfRange(String value)
   at SixLabors.ImageSharp.IO.ChunkedMemoryStream.set_Position(Int64 value)
   at SixLabors.ImageSharp.Image.WithSeekableStream[T](Configuration configuration, Stream stream, Func`2 action)
   at SixLabors.ImageSharp.Image.Identify(Configuration configuration, Stream stream, IImageFormat& format)
   at SixLabors.ImageSharp.Image.Identify(Stream stream)

Steps to Reproduce

using SixLabors.ImageSharp;
using System.IO;
using System.IO.Compression;

internal class Test
{
    public static void Main()
    {
        using (var zipFile = new ZipArchive(new MemoryStream(new byte[] {
            0x50, 0x4B, 0x03, 0x04, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x77, 0xAF,
            0x94, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x6D, 0x79, 0x73, 0x74, 0x65, 0x72,
            0x79, 0x50, 0x4B, 0x01, 0x02, 0x3F, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x77, 0xAF, 0x94, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6D,
            0x79, 0x73, 0x74, 0x65, 0x72, 0x79, 0x0A, 0x00, 0x20, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x46, 0x82, 0xFF, 0x91, 0x27, 0xF6,
            0xD7, 0x01, 0x55, 0xA1, 0xF9, 0x91, 0x27, 0xF6, 0xD7, 0x01, 0x55, 0xA1,
            0xF9, 0x91, 0x27, 0xF6, 0xD7, 0x01, 0x50, 0x4B, 0x05, 0x06, 0x00, 0x00,
            0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x59, 0x00, 0x00, 0x00, 0x25, 0x00,
            0x00, 0x00, 0x00, 0x00 })))
        using (var stream = zipFile.Entries[0].Open())
            Image.Identify(stream);
    }
}

System Configuration

  • ImageSharp version: 2.0.0-alpha.0.140
  • Other ImageSharp packages and versions: N/A
  • Environment (Operating system, version and so on): Windows 10
  • .NET Framework version: 4.8.1
  • Additional information: N/A

Activity

  1. JimBobSquarePants commented on Dec 21, 2021

    @JimBobSquarePants
    Member

    Thanks for your detailed report! Really useful.

    We'll check for zero length early and throw a NotSupportedException.

  2. carlreinke commented on Dec 21, 2021

    @carlreinke
    Author

    Rather than throwing an exception for this case, I'd expect ChunkedMemoryStream to support seeking to positions at and beyond the end of the stream. Then you get the same behavior for empty (and also non-empty) seekable and unseekable streams (ex. Image.Identify would return null in this case).

    See the Remarks for Stream.Position:

    Seeking to any location beyond the length of the stream is supported.

  3. added a commit that references this issue on Dec 21, 2021
    c57bf82
  4. JimBobSquarePants commented on Dec 21, 2021

    @JimBobSquarePants
    Member

    @carlreinke Yep, you're correct. Docs are explicit in this regard. I've opened #1907 to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions