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

Added missing length check that caused an ArgumentNullException (#750) #753

Merged
merged 4 commits into from
Oct 27, 2018
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
5 changes: 5 additions & 0 deletions src/ImageSharp/Common/Extensions/EncoderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ internal static unsafe class EncoderExtensions
/// <returns>The string.</returns>
public static string GetString(this Encoding encoding, ReadOnlySpan<byte> buffer)
{
if (buffer.Length == 0)
{
return string.Empty;
}

fixed (byte* bytes = buffer)
{
return encoding.GetString(bytes, buffer.Length);
Expand Down
32 changes: 32 additions & 0 deletions tests/ImageSharp.Tests/Common/EncoderExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Text;
using Xunit;

namespace SixLabors.ImageSharp.Tests.Common
{
public class EncoderExtensionsTests
{
[Fact]
public void GetString_EmptyBuffer_ReturnsEmptyString()
{
var buffer = new ReadOnlySpan<byte>();

string result = Encoding.UTF8.GetString(buffer);

Assert.Equal(string.Empty, result);
}

[Fact]
public void GetString_Buffer_ReturnsString()
{
var buffer = new ReadOnlySpan<byte>(new byte[] { 73, 109, 97, 103, 101, 83, 104, 97, 114, 112 });

string result = Encoding.UTF8.GetString(buffer);

Assert.Equal("ImageSharp", result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public partial class JpegDecoderTests
TestImages.Jpeg.Issues.ExifDecodeOutOfRange694,
TestImages.Jpeg.Issues.InvalidEOI695,
TestImages.Jpeg.Issues.ExifResizeOutOfRange696,
TestImages.Jpeg.Issues.InvalidAPP0721
TestImages.Jpeg.Issues.InvalidAPP0721,
TestImages.Jpeg.Issues.ExifGetString750Load,
TestImages.Jpeg.Issues.ExifGetString750Transform
};

public static string[] ProgressiveTestJpegs =
Expand Down
2 changes: 2 additions & 0 deletions tests/ImageSharp.Tests/TestImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ public static class Issues
public const string OrderedInterleavedProgressive723A = "Jpg/issues/Issue723-Ordered-Interleaved-Progressive-A.jpg";
public const string OrderedInterleavedProgressive723B = "Jpg/issues/Issue723-Ordered-Interleaved-Progressive-B.jpg";
public const string OrderedInterleavedProgressive723C = "Jpg/issues/Issue723-Ordered-Interleaved-Progressive-C.jpg";
public const string ExifGetString750Transform = "Jpg/issues/issue750-exif-tranform.jpg";
Copy link
Member Author

Choose a reason for hiding this comment

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

Small typo here. Thanks for adding these images.

Copy link
Member

Choose a reason for hiding this comment

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

No worries, I'll update the reference file name later on, will do for now.

public const string ExifGetString750Load = "Jpg/issues/issue750-exif-load.jpg";
}

public static readonly string[] All = Baseline.All.Concat(Progressive.All).ToArray();
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.