Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Icon/Image from Stream doesn't work for partial reads #80261

Closed
stephentoub opened this issue Jan 5, 2023 · 3 comments · Fixed by #82621
Closed

Icon/Image from Stream doesn't work for partial reads #80261

stephentoub opened this issue Jan 5, 2023 · 3 comments · Fixed by #82621

Comments

@stephentoub
Copy link
Member

stephentoub commented Jan 5, 2023

Repro:

using System.Drawing;

byte[] ico;
using (var hc = new HttpClient())
{
    ico = await new HttpClient().GetByteArrayAsync(@"https://raw.githubusercontent.com/dotnet/runtime/main/src/native/corehost/dotnet/dotnet.ico");
}

var stream = new TrickleStream(ico);
new Icon(stream);

class TrickleStream : MemoryStream
{
    public TrickleStream(byte[] bytes) : base(bytes) { }

    public override int Read(byte[] buffer, int offset, int count) =>
        base.Read(buffer, offset, Math.Min(count, 1));
    public override int Read(Span<byte> buffer) =>
        base.Read(buffer.IsEmpty ? buffer : buffer.Slice(0, 1));
}

This should successfully load the data from the Stream into the icon, but instead it throws an exception.

All of these should be updated to not assume Stream.Read will return as much data as was requested.

f.Read(_iconData, 0, _iconData.Length);

stream.Read(_iconData, 0, _iconData.Length);

stream.Read(_iconData, 0, _iconData.Length);

dataStream.Read(image._rawData, 0, (int)dataStream.Length);

cc: @eerhardt

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jan 5, 2023
@ghost
Copy link

ghost commented Jan 5, 2023

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

Issue Details

Repro:

using System.Drawing;

byte[] ico;
using (var hc = new HttpClient())
{
    ico = await new HttpClient().GetByteArrayAsync(@"https://raw.githubusercontent.com/dotnet/runtime/main/src/native/corehost/dotnet/dotnet.ico");
}

var stream = new TrickleStream(ico);
new Icon(stream);

class TrickleStream : MemoryStream
{
    public TrickleStream(byte[] bytes) : base(bytes) { }

    public override int Read(byte[] buffer, int offset, int count) =>
        base.Read(buffer, offset, Math.Min(count, count));
    public override int Read(Span<byte> buffer) =>
        base.Read(buffer.IsEmpty ? buffer : buffer.Slice(0, 1));
}

This should successfully load the data from the Stream into the icon, but instead it throws an exception.

All of these should be updated to not assume Stream.Read will return as much data as was requested.

f.Read(_iconData, 0, _iconData.Length);

stream.Read(_iconData, 0, _iconData.Length);

stream.Read(_iconData, 0, _iconData.Length);

dataStream.Read(image._rawData, 0, (int)dataStream.Length);

Author: stephentoub
Assignees: -
Labels:

area-System.Drawing

Milestone: -

@stephentoub
Copy link
Member Author

Related:
#69159

@ViktorHofer ViktorHofer removed the untriaged New issue has not been triaged by the area owner label Jan 23, 2023
@ViktorHofer ViktorHofer added this to the 8.0.0 milestone Jan 23, 2023
@ViktorHofer
Copy link
Member

cc @JeremyKuhne

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Feb 24, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Mar 2, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Apr 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants