Re: Here's a git patch for operations/external/png-load.c

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 11/16/2010 11:22 AM, Martin Nordholts wrote:
Hi

The patch doesn't apply cleanly for me (sorry for messy formating):
Sorry about that. I think I may know why, I was two checkins away from trunk and the patch only reflected one. This one should apply, but if not please let me know.

Patrick
>From c9192c9086539a6840e4f3cc90ddd71df1a21ec6 Mon Sep 17 00:00:00 2001
From: Patrick Horgan <phorgan1@xxxxxxxxx>
Date: Fri, 19 Nov 2010 18:31:34 -0800
Subject: [PATCH] Check fread return status for error so that if the file is short, then
 will be able to give a more meaningful error.  Also abstract current
 code out of two routines that open the file and read it.

---
 operations/external/png-load.c |   68 ++++++++++++++++++++++++----------------
 1 files changed, 41 insertions(+), 27 deletions(-)

diff --git a/operations/external/png-load.c b/operations/external/png-load.c
index 9f0d210..b0a527f 100644
--- a/operations/external/png-load.c
+++ b/operations/external/png-load.c
@@ -34,6 +34,43 @@ gegl_chant_file_path (path, _("File"), "", _("Path of file to load."))
 #include "gegl-chant.h"
 #include <png.h>
 
+static FILE * open_png(const gchar *path)
+{
+  FILE *infile;
+  const size_t hdr_size=8;
+  size_t hdr_read_size;
+  unsigned char header[hdr_size];
+
+  if (!strcmp (path, "-"))
+    {
+      infile = stdin;
+    }
+  else
+    {
+      infile = fopen (path, "rb");
+    }
+  if (!infile)
+    {
+      return infile;
+    }
+
+  if((hdr_read_size=fread(header, 1, hdr_size, infile))!=hdr_size)
+    {
+      fclose(infile);
+      g_warning ("%s is too short for a png file, only %d bytes.",
+						  path, hdr_read_size);
+      return NULL;
+    }
+
+  if (png_sig_cmp (header, 0, hdr_size))
+    {
+      fclose (infile);
+      g_warning ("%s is not a png file", path);
+      return NULL;
+    }
+  return infile;
+}
+
 static gint
 gegl_buffer_import_png (GeglBuffer  *gegl_buffer,
                         const gchar *path,
@@ -53,7 +90,6 @@ gegl_buffer_import_png (GeglBuffer  *gegl_buffer,
   FILE          *infile;
   png_structp    load_png_ptr;
   png_infop      load_info_ptr;
-  unsigned char  header[8];
   guchar        *pixels;
   /*png_bytep     *rows;*/
 
@@ -61,24 +97,10 @@ gegl_buffer_import_png (GeglBuffer  *gegl_buffer,
   unsigned   int i;
   png_bytep  *row_p = NULL;
 
-  if (!strcmp (path, "-"))
-    {
-      infile = stdin;
-    }
-  else
-    {
-      infile = fopen (path, "rb");
-    }
-  if (!infile)
-    {
-      return -1;
-    }
+  infile = open_png (path);
 
-  fread (header, 1, 8, infile);
-  if (png_sig_cmp (header, 0, 8))
+  if (!infile)
     {
-      fclose (infile);
-      g_warning ("%s is not a png file", path);
       return -1;
     }
 
@@ -239,21 +261,13 @@ static gint query_png (const gchar *path,
   FILE         *infile;
   png_structp   load_png_ptr;
   png_infop     load_info_ptr;
-  unsigned char header[8];
 
   png_bytep  *row_p = NULL;
 
-  infile = fopen (path, "rb");
-  if (!infile)
-    {
-      return -1;
-    }
+  infile = open_png (path);
 
-  fread (header, 1, 8, infile);
-  if (png_sig_cmp (header, 0, 8))
+  if (!infile)
     {
-      fclose (infile);
-      g_warning ("%s is not a png file", path);
       return -1;
     }
 
-- 
1.7.1

_______________________________________________
Gegl-developer mailing list
Gegl-developer@xxxxxxxxxxxxxxxxxxxxxx
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gegl-developer

[Index of Archives]     [Yosemite News]     [Yosemite Photos]     [gtk]     [GIMP Users]     [KDE]     [Gimp's Home]     [Gimp on Windows]     [Steve's Art]

  Powered by Linux