Skip to content

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

Description

@stephentoub

Repro:

using System.Drawing;

byte[] ico;
using (var hc = new HttpClient())
{
    ico = await new HttpClient().GetByteArrayAsync(@"https://github.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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions