5246fc
From 3e38d5ad724a042eebadcba8c2d57b0f48b7a8c7 Mon Sep 17 00:00:00 2001
5246fc
From: Jason Crain <jcrain@src.gnome.org>
5246fc
Date: Mon, 15 Apr 2019 23:06:36 -0600
5246fc
Subject: [PATCH] tiff: Handle failure from TIFFReadRGBAImageOriented
5246fc
5246fc
The TIFFReadRGBAImageOriented function returns zero if it was unable to
5246fc
read the image. Return NULL in this case instead of displaying
5246fc
uninitialized memory.
5246fc
5246fc
Fixes #1129
5246fc
---
5246fc
 backend/tiff/tiff-document.c | 28 ++++++++++++++++++----------
5246fc
 1 file changed, 18 insertions(+), 10 deletions(-)
5246fc
5246fc
diff --git a/backend/tiff/tiff-document.c b/backend/tiff/tiff-document.c
5246fc
index 7715031b..38bb3bd8 100644
5246fc
--- a/backend/tiff/tiff-document.c
5246fc
+++ b/backend/tiff/tiff-document.c
5246fc
@@ -292,18 +292,22 @@ tiff_document_render (EvDocument      *document,
5246fc
 		g_warning("Failed to allocate memory for rendering.");
5246fc
 		return NULL;
5246fc
 	}
5246fc
-	
5246fc
+
5246fc
+	if (!TIFFReadRGBAImageOriented (tiff_document->tiff,
5246fc
+					width, height,
5246fc
+					(uint32 *)pixels,
5246fc
+					orientation, 0)) {
5246fc
+		g_warning ("Failed to read TIFF image.");
5246fc
+		g_free (pixels);
5246fc
+		return NULL;
5246fc
+	}
5246fc
+
5246fc
 	surface = cairo_image_surface_create_for_data (pixels,
5246fc
 						       CAIRO_FORMAT_RGB24,
5246fc
 						       width, height,
5246fc
 						       rowstride);
5246fc
 	cairo_surface_set_user_data (surface, &key,
5246fc
 				     pixels, (cairo_destroy_func_t)g_free);
5246fc
-
5246fc
-	TIFFReadRGBAImageOriented (tiff_document->tiff,
5246fc
-				   width, height,
5246fc
-				   (uint32 *)pixels,
5246fc
-				   orientation, 0);
5246fc
 	pop_handlers ();
5246fc
 
5246fc
 	/* Convert the format returned by libtiff to
5246fc
@@ -384,13 +388,17 @@ tiff_document_get_thumbnail (EvDocument      *document,
5246fc
 	if (!pixels)
5246fc
 		return NULL;
5246fc
 	
5246fc
+	if (!TIFFReadRGBAImageOriented (tiff_document->tiff,
5246fc
+					width, height,
5246fc
+					(uint32 *)pixels,
5246fc
+					ORIENTATION_TOPLEFT, 0)) {
5246fc
+		g_free (pixels);
5246fc
+		return NULL;
5246fc
+	}
5246fc
+
5246fc
 	pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8, 
5246fc
 					   width, height, rowstride,
5246fc
 					   (GdkPixbufDestroyNotify) g_free, NULL);
5246fc
-	TIFFReadRGBAImageOriented (tiff_document->tiff,
5246fc
-				   width, height,
5246fc
-				   (uint32 *)pixels,
5246fc
-				   ORIENTATION_TOPLEFT, 0);
5246fc
 	pop_handlers ();
5246fc
 
5246fc
 	ev_render_context_compute_scaled_size (rc, width, height * (x_res / y_res),
5246fc
-- 
5246fc
2.21.0
5246fc