summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlbrecht Schlosser <albrechts.fltk@online.de>2017-09-10 11:38:23 +0000
committerAlbrecht Schlosser <albrechts.fltk@online.de>2017-09-10 11:38:23 +0000
commitb0a222364225bd49eeff41e6f0dc380804f7ac4c (patch)
tree6f12fa769cded427d5c732f3acaa93bd8b63af86 /src
parent4a8e829f09034f0353a03a6b0fb1c15a81c4d9b8 (diff)
Fix compiler warning and potential error in PNG error handling.
Compilation with gcc -Wall -Wextra displayed the following warning: Fl_PNG_Image.cxx: In member function ‘void Fl_PNG_Image::load_png_(const char*, const unsigned char*, int)’: Fl_PNG_Image.cxx:118:9: warning: variable ‘fp’ might be clobbered by ‘longjmp’ or ‘vfork’ [-Wclobbered] Making the variable static and initializing it properly avoids this potential error in the PNG lib's error handling. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12439 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src')
-rw-r--r--src/Fl_PNG_Image.cxx34
1 files changed, 20 insertions, 14 deletions
diff --git a/src/Fl_PNG_Image.cxx b/src/Fl_PNG_Image.cxx
index aa9635ee9..a8e253ad0 100644
--- a/src/Fl_PNG_Image.cxx
+++ b/src/Fl_PNG_Image.cxx
@@ -6,6 +6,8 @@
// Copyright 1997-2012 by Easy Software Products.
// Image support by Matthias Melcher, Copyright 2000-2009.
//
+// Copyright 2013-2017 by Bill Spitzak and others.
+//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
@@ -31,9 +33,10 @@
#include <FL/Fl_System_Driver.H>
#include <FL/Fl_PNG_Image.H>
#include <FL/Fl_Shared_Image.H>
+#include <FL/fl_utf8.h>
+
#include <stdio.h>
#include <stdlib.h>
-#include <FL/fl_utf8.h>
#if defined(HAVE_LIBPNG) && defined(HAVE_LIBZ)
extern "C"
@@ -90,14 +93,14 @@ Fl_PNG_Image::Fl_PNG_Image (const char *filename): Fl_RGB_Image(0,0,0)
}
-/**
+/**
\brief Constructor that reads a PNG image from memory.
Construct an image from a block of memory inside the application. Fluid offers
"binary Data" chunks as a great way to add image data into the C++ source code.
- name_png can be NULL. If a name is given, the image is added to the list of
+ name_png can be NULL. If a name is given, the image is added to the list of
shared images (see: Fl_Shared_Image) and will be available by that name.
-
+
\param name_png A name given to this image or NULL
\param buffer Pointer to the start of the PNG image in memory
\param maxsize Size in bytes of the memory buffer containing the PNG image
@@ -112,15 +115,19 @@ Fl_PNG_Image::Fl_PNG_Image (
void Fl_PNG_Image::load_png_(const char *name_png, const unsigned char *buffer_png, int maxsize)
{
#if defined(HAVE_LIBPNG) && defined(HAVE_LIBZ)
- int i; // Looping var
- FILE *fp = NULL; // File pointer
- int channels; // Number of color channels
- png_structp pp; // PNG read pointer
- png_infop info = 0; // PNG info pointers
- png_bytep *rows;// PNG row pointers
+ int i; // Looping var
+ int channels; // Number of color channels
+ png_structp pp; // PNG read pointer
+ png_infop info = 0; // PNG info pointers
+ png_bytep *rows; // PNG row pointers
fl_png_memory png_mem_data;
int from_memory = (buffer_png != NULL); // true if reading image from memory
+ // Note: The file pointer fp must not be an automatic (stack) variable
+ // to avoid potential clobbering by setjmp/longjmp (gcc: [-Wclobbered]).
+ static FILE *fp; // intentionally initialized separately below
+ fp = NULL; // always initialize file pointer
+
if (!from_memory) {
if ((fp = fl_fopen(name_png, "rb")) == NULL) {
ld(ERR_FILE_ACCESS);
@@ -139,9 +146,8 @@ void Fl_PNG_Image::load_png_(const char *name_png, const unsigned char *buffer_p
w(0); h(0); d(0); ld(ERR_FORMAT);
return;
}
-
- if (setjmp(png_jmpbuf(pp)))
- {
+
+ if (setjmp(png_jmpbuf(pp))) {
png_destroy_read_struct(&pp, &info, NULL);
if (!from_memory) fclose(fp);
Fl::warning("PNG file or data \"%s\" is too large or contains errors!\n", display_name);
@@ -157,7 +163,7 @@ void Fl_PNG_Image::load_png_(const char *name_png, const unsigned char *buffer_p
png_set_read_fn (pp, (png_voidp) &png_mem_data, png_read_data_from_mem);
} else {
png_init_io(pp, fp); // Initialize the PNG file read "engine"...
- }
+ }
// Get the image dimensions and convert to grayscale or RGB...
png_read_info(pp, info);