Skip to content

Commit

Permalink
Added missing length check that caused an ArgumentNullException (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Oct 26, 2018
1 parent 00070f4 commit 013d58e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
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 null;
}

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_ReturnsNull()
{
var buffer = new ReadOnlySpan<byte>();

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

Assert.Null(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);
}
}
}

0 comments on commit 013d58e

Please sign in to comment.