From 6cce9311e70b969cbcc6e3e1e74ae1756ed02d5b Mon Sep 17 00:00:00 2001
From: Matthias Clasen <mclasen@redhat.com>
Date: Mon, 30 Mar 2026 12:21:56 -0400
Subject: [PATCH] jpeg: Reject unsupported number of components

This condition was already checked for incremental loading.
This commit adds the same check in the nonincremental
code path.

Closes: #304
---
 gdk-pixbuf/io-jpeg.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff -urp gdk-pixbuf-2.40.0.orig/gdk-pixbuf/io-jpeg.c gdk-pixbuf-2.40.0/gdk-pixbuf/io-jpeg.c
--- gdk-pixbuf-2.40.0.orig/gdk-pixbuf/io-jpeg.c	2026-04-09 12:52:22.942402124 -0500
+++ gdk-pixbuf-2.40.0/gdk-pixbuf/io-jpeg.c	2026-04-09 13:14:17.649628943 -0500
@@ -626,6 +626,18 @@ gdk_pixbuf__jpeg_image_load (FILE *f, GE
 	cinfo.do_fancy_upsampling = FALSE;
 	cinfo.do_block_smoothing = FALSE;
 
+        /* Reject unsupported component counts */
+        if (cinfo.output_components != 3 && cinfo.output_components != 4 &&
+            !(cinfo.output_components == 1 &&
+              cinfo.out_color_space == JCS_GRAYSCALE)) {
+                g_set_error (error,
+                             GDK_PIXBUF_ERROR,
+                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                             _("Unsupported number of color components (%d)"),
+                             cinfo.output_components);
+                goto out;
+        }
+
 	pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 
 				 cinfo.out_color_components == 4 ? TRUE : FALSE, 
 				 8, cinfo.output_width, cinfo.output_height);
