Skip to content

Commit 911d8d7

Browse files
Merge branch 'main' into js/decode-sanitation
2 parents 2ac18d8 + 923c5d2 commit 911d8d7

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,15 @@ public static int Write(EncodedString encodedString, Span<byte> destination)
8080
}
8181

8282
public static unsafe int Write(Encoding encoding, string value, Span<byte> destination)
83+
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER || NET
84+
=> encoding.GetBytes(value.AsSpan(), destination);
85+
#else
8386
{
87+
if (value.Length == 0)
88+
{
89+
return 0;
90+
}
91+
8492
fixed (char* c = value)
8593
{
8694
fixed (byte* b = destination)
@@ -89,6 +97,7 @@ public static unsafe int Write(Encoding encoding, string value, Span<byte> desti
8997
}
9098
}
9199
}
100+
#endif
92101

93102
private static bool TryDetect(ReadOnlySpan<byte> buffer, out CharacterCode code)
94103
{
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System;
5+
using System.IO;
6+
using SixLabors.ImageSharp.Formats.Jpeg;
7+
using SixLabors.ImageSharp.PixelFormats;
8+
9+
using Xunit;
10+
11+
namespace SixLabors.ImageSharp.Tests.Formats.Jpg
12+
{
13+
[Trait("Format", "Jpg")]
14+
public partial class JpegEncoderTests
15+
{
16+
[Theory]
17+
[WithFile(TestImages.Jpeg.Issues.ValidExifArgumentNullExceptionOnEncode, PixelTypes.Rgba32)]
18+
public void Encode_WithValidExifProfile_DoesNotThrowException<TPixel>(TestImageProvider<TPixel> provider)
19+
where TPixel : unmanaged, IPixel<TPixel>
20+
{
21+
Exception ex = Record.Exception(() =>
22+
{
23+
var encoder = new JpegEncoder();
24+
var stream = new MemoryStream();
25+
26+
using Image<TPixel> image = provider.GetImage(JpegDecoder);
27+
image.Save(stream, encoder);
28+
});
29+
30+
Assert.Null(ex);
31+
}
32+
}
33+
}

tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
namespace SixLabors.ImageSharp.Tests.Formats.Jpg
2121
{
2222
[Trait("Format", "Jpg")]
23-
public class JpegEncoderTests
23+
public partial class JpegEncoderTests
2424
{
2525
private static JpegEncoder JpegEncoder => new();
2626

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ public static class Issues
264264
public const string InvalidIptcTag = "Jpg/issues/Issue1942InvalidIptcTag.jpg";
265265
public const string Issue2057App1Parsing = "Jpg/issues/Issue2057-App1Parsing.jpg";
266266
public const string ExifNullArrayTag = "Jpg/issues/issue-2056-exif-null-array.jpg";
267+
public const string ValidExifArgumentNullExceptionOnEncode = "Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg";
267268

268269
public static class Fuzz
269270
{
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)