changeset 35302:990a80ea52d3

Fix leaks in case of error.
author reimar
date Sat, 10 Nov 2012 13:25:25 +0000
parents 8f6d3f8ffa61
children aad2bda4f65f
files libmenu/menu.c
diffstat 1 files changed, 8 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/libmenu/menu.c	Sat Nov 10 13:19:29 2012 +0000
+++ b/libmenu/menu.c	Sat Nov 10 13:25:25 2012 +0000
@@ -232,15 +232,15 @@
 int menu_init(struct MPContext *mpctx, char* cfg_file) {
   char* buffer = NULL;
   int bl = BUF_STEP, br = 0;
-  int f, fd;
+  int f = 0, fd = -1;
 #ifndef CONFIG_FREETYPE
   if(vo_font == NULL)
-    return 0;
+    goto out;
 #endif
   fd = open(cfg_file, O_RDONLY);
   if(fd < 0) {
     mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_CantOpenConfigFile,cfg_file);
-    return 0;
+    goto out;
   }
   buffer = malloc(bl);
   while(1) {
@@ -248,9 +248,7 @@
     if(bl - br < BUF_MIN) {
       if(bl >= BUF_MAX) {
 	mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_ConfigFileIsTooBig,BUF_MAX/1024);
-	close(fd);
-	free(buffer);
-	return 0;
+	goto out;
       }
       bl += BUF_STEP;
       buffer = realloc(buffer,bl);
@@ -261,14 +259,15 @@
   }
   if(!br) {
     mp_msg(MSGT_GLOBAL,MSGL_WARN,MSGTR_LIBMENU_ConfigFileIsEmpty);
-    return 0;
+    goto out;
   }
   buffer[br-1] = '\0';
 
-  close(fd);
-
   menu_ctx = mpctx;
   f = menu_parse_config(buffer);
+
+out:
+  if (fd != -1) close(fd);
   free(buffer);
   return f;
 }