Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/libraries/System.Drawing.Common/src/System/Drawing/Icon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,20 @@ public Icon(Stream stream, int width, int height) : this()
ArgumentNullException.ThrowIfNull(stream);

_iconData = new byte[(int)stream.Length];
stream.Read(_iconData, 0, _iconData.Length);
#if NET7_0_OR_GREATER
stream.ReadExactly(_iconData);
#else
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to backport this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. It's been like this forever and to my knowledge no one has complained. I found it via an audit rather than feedback.

int totalRead = 0;
while (totalRead < _iconData.Length)
{
int bytesRead = stream.Read(_iconData, totalRead, _iconData.Length - totalRead);
if (bytesRead <= 0)
{
throw new EndOfStreamException();
}
totalRead += bytesRead;
}
#endif
Initialize(width, height);
}

Expand Down
17 changes: 17 additions & 0 deletions src/libraries/System.Drawing.Common/tests/IconTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ public void Ctor_Stream()
}
}

[ConditionalFact(Helpers.IsDrawingSupported)]
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "Bug fix in core")]
public void Ctor_Stream_Trickled()
{
var stream = new TrickleStream(File.ReadAllBytes(Helpers.GetTestBitmapPath("48x48_multiple_entries_4bit.ico")));
var icon = new Icon(stream);
Assert.Equal(32, icon.Width);
Assert.Equal(32, icon.Height);
Assert.Equal(new Size(32, 32), icon.Size);
}

private sealed 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));
}

[ConditionalTheory(Helpers.IsDrawingSupported)]
[MemberData(nameof(Size_TestData))]
public void Ctor_Stream_Width_Height(string fileName, Size size, Size expectedSize)
Expand Down