imageflow-dotnet icon indicating copy to clipboard operation
imageflow-dotnet copied to clipboard

Incorrect exception when passing stream that does not expose a buffer

Open mpec opened this issue 1 year ago • 2 comments

When passing a MemoryStream that does not expose buffer via this method: https://learn.microsoft.com/en-us/dotnet/api/system.io.memorystream.trygetbuffer?view=net-8.0 I get the following exception:

System.OverflowException:
   at Imageflow.Fluent.BufferedStreamSource.TryGetWrittenMemory (Imageflow.Net, Version=0.13.1.0, Culture=neutral, PublicKeyToken=null)
   at Imageflow.Fluent.BufferedStreamSource+<BorrowReadOnlyMemoryAsync>d__8.MoveNext (Imageflow.Net, Version=0.13.1.0, Culture=neutral, PublicKeyToken=null)
[snip]
, Message: Streams cannot exceed 2GB 

When running code:

var memorySource = BufferedStreamSource.BorrowEntireStream(sanitizedStream);
var info = await ImageJob.GetImageInfoAsync(memorySource, SourceLifetime.Borrowed);

I think this should be validated when creating BufferedStreamSource and more appropriate exception be thrown.

mpec avatar Apr 08 '24 14:04 mpec

To repro and fix this I need the stream class you're using.

On Mon, Apr 8, 2024, 8:11 AM Marcin @.***> wrote:

When passing a MemoryStream that does not expose buffer via this method: https://learn.microsoft.com/en-us/dotnet/api/system.io.memorystream.trygetbuffer?view=net-8.0 I get the following exception:

System.OverflowException: at Imageflow.Fluent.BufferedStreamSource.TryGetWrittenMemory (Imageflow.Net, Version=0.13.1.0, Culture=neutral, PublicKeyToken=null) at Imageflow.Fluent.BufferedStreamSource+<BorrowReadOnlyMemoryAsync>d__8.MoveNext (Imageflow.Net, Version=0.13.1.0, Culture=neutral, PublicKeyToken=null) [snip] , Message: Streams cannot exceed 2GB

When running code:

var memorySource = BufferedStreamSource.BorrowEntireStream(sanitizedStream); var info = await ImageJob.GetImageInfoAsync(memorySource, SourceLifetime.Borrowed);

I think this should be validated when creating BufferedStreamSource and more appropriate exception be thrown.

— Reply to this email directly, view it on GitHub https://github.com/imazen/imageflow-dotnet/issues/58, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA2LH53LOZPZEYNPJTC5FTY4KQR3AVCNFSM6AAAAABF4ZNXQGVHI2DSMVQWIX3LMV43ASLTON2WKOZSGIZTCMZTGM2DENQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

lilith avatar Apr 08 '24 14:04 lilith

It is a MemoryStream created by https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent.readasstreamasync?view=net-8.0

mpec avatar Apr 08 '24 14:04 mpec

Thanks. This should be fixed in the next release, and will "just work" - it is supposed to fall back to using read methods

lilith avatar May 15 '24 21:05 lilith