Skip to content

Commit 0b34bca

Browse files
Merge pull request #3022 from SixLabors/js/fix-2948
Explicitly handle missing SOS marker.
2 parents b078cb9 + 143c3bb commit 0b34bca

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

.github/workflows/build-and-test.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ jobs:
6262
needs: WarmLFS
6363
strategy:
6464
matrix:
65-
isARM:
66-
- ${{ contains(github.event.pull_request.labels.*.name, 'arch:arm32') || contains(github.event.pull_request.labels.*.name, 'arch:arm64') }}
6765
options:
6866
- os: ubuntu-latest
6967
framework: net9.0
@@ -121,10 +119,6 @@ jobs:
121119
sdk: 8.0.x
122120
runtime: -x64
123121
codecov: false
124-
exclude:
125-
- isARM: false
126-
options:
127-
os: buildjet-4vcpu-ubuntu-2204-arm
128122

129123
runs-on: ${{ matrix.options.os }}
130124

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ internal sealed class JpegDecoderCore : ImageDecoderCore, IRawJpegData
7171
/// </summary>
7272
private bool hasAdobeMarker;
7373

74+
/// <summary>
75+
/// Whether the image has a SOS marker.
76+
/// </summary>
77+
private bool hasSOSMarker;
78+
7479
/// <summary>
7580
/// Contains information about the JFIF marker.
7681
/// </summary>
@@ -197,6 +202,12 @@ protected override Image<TPixel> Decode<TPixel>(BufferedReadStream stream, Cance
197202
{
198203
using SpectralConverter<TPixel> spectralConverter = new(this.configuration, this.resizeMode == JpegDecoderResizeMode.ScaleOnly ? null : this.Options.TargetSize);
199204
this.ParseStream(stream, spectralConverter, cancellationToken);
205+
206+
if (!this.hasSOSMarker)
207+
{
208+
JpegThrowHelper.ThrowInvalidImageContentException("Missing SOS marker.");
209+
}
210+
200211
this.InitExifProfile();
201212
this.InitIccProfile();
202213
this.InitIptcProfile();
@@ -215,6 +226,12 @@ protected override Image<TPixel> Decode<TPixel>(BufferedReadStream stream, Cance
215226
protected override ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken)
216227
{
217228
this.ParseStream(stream, spectralConverter: null, cancellationToken);
229+
230+
if (!this.hasSOSMarker)
231+
{
232+
JpegThrowHelper.ThrowInvalidImageContentException("Missing SOS marker.");
233+
}
234+
218235
this.InitExifProfile();
219236
this.InitIccProfile();
220237
this.InitIptcProfile();
@@ -403,6 +420,8 @@ internal void ParseStream(BufferedReadStream stream, SpectralConverter spectralC
403420
break;
404421

405422
case JpegConstants.Markers.SOS:
423+
424+
this.hasSOSMarker = true;
406425
if (!metadataOnly)
407426
{
408427
this.ProcessStartOfScanMarker(stream, markerContentByteSize);

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,20 @@ public void Decode_RGB_ICC_Jpeg<TPixel>(TestImageProvider<TPixel> provider)
417417
image.DebugSave(provider);
418418
image.CompareToReferenceOutput(provider);
419419
}
420+
421+
// https://github.com/SixLabors/ImageSharp/issues/2948
422+
[Theory]
423+
[WithFile(TestImages.Jpeg.Issues.Issue2948, PixelTypes.Rgb24)]
424+
public void Issue2948_No_SOS_Decode_Throws_InvalidImageContentException<TPixel>(TestImageProvider<TPixel> provider)
425+
where TPixel : unmanaged, IPixel<TPixel>
426+
=> Assert.Throws<InvalidImageContentException>(() =>
427+
{
428+
using Image<TPixel> image = provider.GetImage(JpegDecoder.Instance);
429+
});
430+
431+
// https://github.com/SixLabors/ImageSharp/issues/2948
432+
[Theory]
433+
[InlineData(TestImages.Jpeg.Issues.Issue2948)]
434+
public void Issue2948_No_SOS_Identify_Throws_InvalidImageContentException(string imagePath)
435+
=> Assert.Throws<InvalidImageContentException>(() => _ = Image.Identify(TestFile.Create(imagePath).Bytes));
420436
}

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ public static class Issues
348348
public const string Issue2638 = "Jpg/issues/Issue2638.jpg";
349349
public const string Issue2758 = "Jpg/issues/issue-2758.jpg";
350350
public const string Issue2857 = "Jpg/issues/issue-2857-subsub-ifds.jpg";
351+
public const string Issue2948 = "Jpg/issues/issue-2948-sos.jpg";
351352

352353
public static class Fuzz
353354
{
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)