diff options
| author | Albrecht Schlosser <albrechts.fltk@online.de> | 2021-09-28 15:11:55 +0200 |
|---|---|---|
| committer | Albrecht Schlosser <albrechts.fltk@online.de> | 2021-09-28 15:11:55 +0200 |
| commit | 32e02a6e8d0d4323a2214963349b0cb1bc6376da (patch) | |
| tree | 94d5234d10c985da47f68c3dfd84bf42f697eb9b | |
| parent | b6a69db9a67c9353343d16814cbf35c5e3f36946 (diff) | |
Fix potential memory leak in GIF image reader (#271)
This could happen if a read error or end of file was encountered.
| -rw-r--r-- | src/Fl_GIF_Image.cxx | 46 |
1 files changed, 31 insertions, 15 deletions
diff --git a/src/Fl_GIF_Image.cxx b/src/Fl_GIF_Image.cxx index 32a1dabd9..6ae3f6ca1 100644 --- a/src/Fl_GIF_Image.cxx +++ b/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; |
