[cairo-commit] src/cairo-png.c

Chris Wilson ickle at kemper.freedesktop.org
Fri Apr 20 01:53:04 PDT 2007


 src/cairo-png.c |   24 +++++++++++++++++-------
 1 files changed, 17 insertions(+), 7 deletions(-)

New commits:
diff-tree bf957ee0f29ff49970b4791718eccdb92f3d6886 (from d00aa44206f474e11c66c585d09bfd55e747cc5b)
Author: Chris Wilson <chris at chris-wilson.co.uk>
Date:   Fri Mar 23 12:59:38 2007 +0000

    cairo-png - handle short reads
    
    During MT cairo testing we get the error "libpng: Read Error" which is
    emitted should the fread return fewer bytes than asked. However, this is
    not necessarily an error, so double-check the ferror() status before
    raising a png_error().

diff --git a/src/cairo-png.c b/src/cairo-png.c
index 10f801a..a6b2e74 100644
--- a/src/cairo-png.c
+++ b/src/cairo-png.c
@@ -207,8 +207,13 @@ stdio_write_func (png_structp png, png_b
     FILE *fp;
 
     fp = png_get_io_ptr (png);
-    if (fwrite (data, 1, size, fp) != size)
-	png_error(png, "Write Error");
+    while (size) {
+	size_t ret = fwrite (data, 1, size, fp);
+	size -= ret;
+	data += ret;
+	if (size && ferror (fp))
+	    png_error(png, "Write Error");
+    }
 }
 
 /**
@@ -333,14 +338,14 @@ read_png (png_rw_ptr	read_func,
 	  void		*closure)
 {
     cairo_surface_t *surface = (cairo_surface_t*) &_cairo_surface_nil;
-    png_byte *data = NULL;
-    unsigned int i;
     png_struct *png = NULL;
     png_info *info;
+    volatile png_byte *data = NULL;
+    volatile png_byte **row_pointers = NULL;
     png_uint_32 png_width, png_height, stride;
     int depth, color_type, interlace;
+    unsigned int i;
     unsigned int pixel_size;
-    png_byte **row_pointers = NULL;
 
     /* XXX: Perhaps we'll want some other error handlers? */
     png = png_create_read_struct (PNG_LIBPNG_VER_STRING,
@@ -443,8 +448,13 @@ stdio_read_func (png_structp png, png_by
     FILE *fp;
 
     fp = png_get_io_ptr (png);
-    if (fread (data, 1, size, fp) != size)
-	png_error(png, "Read Error");
+    while (size) {
+	size_t ret = fread (data, 1, size, fp);
+	size -= ret;
+	data += ret;
+	if (size && ferror (fp))
+	    png_error(png, "Read Error");
+    }
 }
 
 /**


More information about the cairo-commit mailing list