FLTK logo

[master] 32e02a6 - Fix potential memory leak in GIF image reader (#271)

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[master] 32e02a6 - Fix potential memory leak in GIF image reader (#271) "Albrecht Schlosser" Sep 28, 2021  
 
commit 32e02a6e8d0d4323a2214963349b0cb1bc6376da
Author:     Albrecht Schlosser <albrechts.fltk@online.de>
AuthorDate: Tue Sep 28 15:11:55 2021 +0200
Commit:     Albrecht Schlosser <albrechts.fltk@online.de>
CommitDate: Tue Sep 28 15:11:55 2021 +0200

    Fix potential memory leak in GIF image reader (#271)
    
    This could happen if a read error or end of file was encountered.

 src/Fl_GIF_Image.cxx | 46 +++++++++++++++++++++++++++++++---------------
 1 file changed, 31 insertions(+), 15 deletions(-)

diff --git src/Fl_GIF_Image.cxx src/Fl_GIF_Image.cxx
index 32a1dab..6ae3f6c 100644
--- src/Fl_GIF_Image.cxx
+++ src/Fl_GIF_Image.cxx
@@ -73,6 +73,35 @@
  */
 
 
+/*
+  This small helper function checks for read errors or end of file
+  and does some cleanup if an error was found.
+  It returns true (1) on error, false (0) otherwise.
+*/
+static int gif_error(Fl_Image_Reader &rdr, int line, uchar *Image) {
+  if (rdr.error()) {
+    if (Image)
+      delete[] Image; // delete temporary image array
+
+    Fl::error("[%d] Fl_GIF_Image: %s - unexpected EOF or read error at offset %ld",
+              line, rdr.name(), rdr.tell());
+    return 1;
+  }
+  return 0;
+}
+
+/*
+  This macro is used to check for end of file (EOF) or other read errors.
+  In case of a read error or EOF an error message is issued and the image
+  loading is terminated with error code ERR_FORMAT.
+  This calls gif_error (see above) to avoid code duplication.
+*/
+#define CHECK_ERROR \
+  if (gif_error(rdr, __LINE__, Image)) { \
+    ld(ERR_FORMAT); \
+    return; \
+  }
+
 /**
   This constructor loads a GIF image from the given file.
 
@@ -103,7 +132,6 @@ Fl_GIF_Image::Fl_GIF_Image(const char *filename) :
   }
 }
 
-
 /**
   This constructor loads a GIF image from memory.
 
@@ -153,19 +181,6 @@ Fl_GIF_Image::Fl_GIF_Image(const char *imagename, const unsigned char *data, con
 }
 
 /*
-  This macro can be used to check for end of file (EOF) or other read errors.
-  In case of an error or EOF an error message is issued and the image loading
-  is terminated with error code ERR_FORMAT.
-*/
-#define CHECK_ERROR \
-  if (rdr.error()) { \
-    Fl::error("[%d] Fl_GIF_Image: %s - unexpected EOF or read error at offset %ld", \
-              __LINE__, rdr.name(), rdr.tell()); \
-    ld(ERR_FORMAT); \
-    return; \
-  }
-
-/*
  This method reads GIF image data and creates an RGB or RGBA image. The GIF
  format supports only 1 bit for alpha. To avoid code duplication, we use
  an Fl_Image_Reader that reads data from either a file or from memory.
@@ -173,6 +188,7 @@ Fl_GIF_Image::Fl_GIF_Image(const char *imagename, const unsigned char *data, con
 void Fl_GIF_Image::load_gif_(Fl_Image_Reader &rdr)
 {
   char **new_data;      // Data array
+  uchar *Image = 0L;    // internal temporary image data array
   w(0); h(0);
 
   // printf("\nFl_GIF_Image::load_gif_ : %s\n", rdr.name());
@@ -340,7 +356,7 @@ void Fl_GIF_Image::load_gif_(Fl_Image_Reader &rdr)
 
   // now read the LZW compressed image data
 
-  uchar *Image = new uchar[Width*Height];
+  Image = new uchar[Width*Height];
 
   int YC = 0, Pass = 0; /* Used to de-interlace the picture */
   uchar *p = Image;
Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.