file-bmp: don't leak file pointer when erroring out

This commit is contained in:
Nils Philippsen 2013-11-07 11:58:24 +01:00
parent c028580ae0
commit 6467fbf65d
1 changed files with 25 additions and 22 deletions

View File

@ -193,7 +193,7 @@ ReadBMP (const gchar *name,
gint ColormapSize, rowbytes, Maps; gint ColormapSize, rowbytes, Maps;
gboolean Grey = FALSE; gboolean Grey = FALSE;
guchar ColorMap[256][3]; guchar ColorMap[256][3];
gint32 image_ID; gint32 image_ID = -1;
gchar magick[2]; gchar magick[2];
Bitmap_Channel masks[4]; Bitmap_Channel masks[4];
@ -205,7 +205,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
_("Could not open '%s' for reading: %s"), _("Could not open '%s' for reading: %s"),
gimp_filename_to_utf8 (filename), g_strerror (errno)); gimp_filename_to_utf8 (filename), g_strerror (errno));
return -1; goto out;
} }
gimp_progress_init_printf (_("Opening '%s'"), gimp_progress_init_printf (_("Opening '%s'"),
@ -221,8 +221,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("'%s' is not a valid BMP file"), _("'%s' is not a valid BMP file"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
fclose (fd); goto out;
return -1;
} }
while (!strncmp (magick, "BA", 2)) while (!strncmp (magick, "BA", 2))
@ -232,14 +231,14 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("'%s' is not a valid BMP file"), _("'%s' is not a valid BMP file"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
if (!ReadOK (fd, magick, 2)) if (!ReadOK (fd, magick, 2))
{ {
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("'%s' is not a valid BMP file"), _("'%s' is not a valid BMP file"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
} }
@ -248,7 +247,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("'%s' is not a valid BMP file"), _("'%s' is not a valid BMP file"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
/* bring them to the right byteorder. Not too nice, but it should work */ /* bring them to the right byteorder. Not too nice, but it should work */
@ -263,7 +262,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("'%s' is not a valid BMP file"), _("'%s' is not a valid BMP file"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
Bitmap_File_Head.biSize = ToL (&buffer[0x00]); Bitmap_File_Head.biSize = ToL (&buffer[0x00]);
@ -277,7 +276,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Error reading BMP file header from '%s'"), _("Error reading BMP file header from '%s'"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
Bitmap_Head.biWidth = ToS (&buffer[0x00]); /* 12 */ Bitmap_Head.biWidth = ToS (&buffer[0x00]); /* 12 */
@ -304,7 +303,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Error reading BMP file header from '%s'"), _("Error reading BMP file header from '%s'"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
Bitmap_Head.biWidth = ToL (&buffer[0x00]); /* 12 */ Bitmap_Head.biWidth = ToL (&buffer[0x00]); /* 12 */
@ -332,7 +331,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Error reading BMP file header from '%s'"), _("Error reading BMP file header from '%s'"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
Bitmap_Head.masks[0] = ToL(&buffer[0x00]); Bitmap_Head.masks[0] = ToL(&buffer[0x00]);
@ -361,7 +360,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Error reading BMP file header from '%s'"), _("Error reading BMP file header from '%s'"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
Bitmap_Head.biWidth =ToL (&buffer[0x00]); /* 12 */ Bitmap_Head.biWidth =ToL (&buffer[0x00]); /* 12 */
@ -391,7 +390,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Error reading BMP file header from '%s'"), _("Error reading BMP file header from '%s'"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
Bitmap_Head.biWidth = ToL (&buffer[0x00]); Bitmap_Head.biWidth = ToL (&buffer[0x00]);
@ -425,7 +424,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Error reading BMP file header from '%s'"), _("Error reading BMP file header from '%s'"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
/* Valid bit depth is 1, 4, 8, 16, 24, 32 */ /* Valid bit depth is 1, 4, 8, 16, 24, 32 */
@ -445,7 +444,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("'%s' is not a valid BMP file"), _("'%s' is not a valid BMP file"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
/* There should be some colors used! */ /* There should be some colors used! */
@ -466,7 +465,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("'%s' is not a valid BMP file"), _("'%s' is not a valid BMP file"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
/* biHeight may be negative, but G_MININT32 is dangerous because: /* biHeight may be negative, but G_MININT32 is dangerous because:
@ -477,7 +476,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("'%s' is not a valid BMP file"), _("'%s' is not a valid BMP file"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
if (Bitmap_Head.biPlanes != 1) if (Bitmap_Head.biPlanes != 1)
@ -485,7 +484,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("'%s' is not a valid BMP file"), _("'%s' is not a valid BMP file"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
if (Bitmap_Head.biClrUsed > 256) if (Bitmap_Head.biClrUsed > 256)
@ -493,7 +492,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("'%s' is not a valid BMP file"), _("'%s' is not a valid BMP file"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
/* protect against integer overflows caused by malicious BMPs */ /* protect against integer overflows caused by malicious BMPs */
@ -505,7 +504,7 @@ ReadBMP (const gchar *name,
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("'%s' is not a valid BMP file"), _("'%s' is not a valid BMP file"),
gimp_filename_to_utf8 (filename)); gimp_filename_to_utf8 (filename));
return -1; goto out;
} }
/* Windows and OS/2 declare filler so that rows are a multiple of /* Windows and OS/2 declare filler so that rows are a multiple of
@ -533,7 +532,7 @@ ReadBMP (const gchar *name,
#endif #endif
/* Get the Colormap */ /* Get the Colormap */
if (!ReadColorMap (fd, ColorMap, ColormapSize, Maps, &Grey)) if (!ReadColorMap (fd, ColorMap, ColormapSize, Maps, &Grey))
return -1; goto out;
} }
fseek (fd, Bitmap_File_Head.bfOffs, SEEK_SET); fseek (fd, Bitmap_File_Head.bfOffs, SEEK_SET);
@ -552,7 +551,7 @@ ReadBMP (const gchar *name,
error); error);
if (image_ID < 0) if (image_ID < 0)
return -1; goto out;
if (Bitmap_Head.biXPels > 0 && Bitmap_Head.biYPels > 0) if (Bitmap_Head.biXPels > 0 && Bitmap_Head.biYPels > 0)
{ {
@ -574,6 +573,10 @@ ReadBMP (const gchar *name,
if (Bitmap_Head.biHeight < 0) if (Bitmap_Head.biHeight < 0)
gimp_image_flip (image_ID, GIMP_ORIENTATION_VERTICAL); gimp_image_flip (image_ID, GIMP_ORIENTATION_VERTICAL);
out:
if (fd)
fclose (fd);
return image_ID; return image_ID;
} }