Fix static analysis issues 08/263508/2
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 6 Sep 2021 00:39:11 +0000 (09:39 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 6 Sep 2021 00:41:08 +0000 (09:41 +0900)
- Fixes resource leak
- Fixes uninitialized pointer read

Change-Id: I261b92e787162dd54983267ac6aa80d0c9e038ad
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
xdgmime/src/xdgmime.c
xdgmime/src/xdgmimecache.c
xdgmime/src/xdgmimeglobs2.c

index f898cc1..03ac931 100644 (file)
@@ -553,22 +553,25 @@ xdg_mime_get_mime_type_for_file (const char  *file_name,
 
   file = fopen (file_name, "r");
   if (file == NULL)
-    {
-      free (data);
-      return XDG_MIME_TYPE_UNKNOWN;
-    }
+    return XDG_MIME_TYPE_UNKNOWN;
 
   if (!statbuf)
     {
       fd = fileno(file);
       if (fstat (fd, &buf) != 0)
-       return XDG_MIME_TYPE_UNKNOWN;
+        {
+          fclose(file);
+          return XDG_MIME_TYPE_UNKNOWN;
+        }
 
       statbuf = &buf;
     }
 
   if (!S_ISREG (statbuf->st_mode))
-    return XDG_MIME_TYPE_UNKNOWN;
+    {
+      fclose(file);
+      return XDG_MIME_TYPE_UNKNOWN;
+    }
 
   /* FIXME: Need to make sure that max_extent isn't totally broken.  This could
    * be large and need getting from a stream instead of just reading it all
@@ -576,8 +579,11 @@ xdg_mime_get_mime_type_for_file (const char  *file_name,
   max_extent = _xdg_mime_magic_get_buffer_extents (global_magic);
   data = malloc (max_extent);
   if (data == NULL)
-    return XDG_MIME_TYPE_UNKNOWN;
-        
+    {
+      fclose(file);
+      return XDG_MIME_TYPE_UNKNOWN;
+    }
+
   bytes_read = fread (data, 1, max_extent, file);
   if (ferror (file))
     {
index dd52f21..08188b5 100644 (file)
@@ -745,22 +745,25 @@ _xdg_mime_cache_get_mime_type_for_file (const char  *file_name,
 
   file = fopen (file_name, "r");
   if (file == NULL)
-    {
-      free (data);
-      return XDG_MIME_TYPE_UNKNOWN;
-    }
+    return XDG_MIME_TYPE_UNKNOWN;
 
   if (!statbuf)
     {
       fd = fileno(file);
       if (fstat (fd, &buf) != 0)
-       return XDG_MIME_TYPE_UNKNOWN;
+        {
+          fclose(file);
+          return XDG_MIME_TYPE_UNKNOWN;
+        }
 
       statbuf = &buf;
     }
 
   if (!S_ISREG (statbuf->st_mode))
-    return XDG_MIME_TYPE_UNKNOWN;
+    {
+      fclose(file);
+      return XDG_MIME_TYPE_UNKNOWN;
+    }
 
   /* FIXME: Need to make sure that max_extent isn't totally broken.  This could
    * be large and need getting from a stream instead of just reading it all
@@ -768,7 +771,10 @@ _xdg_mime_cache_get_mime_type_for_file (const char  *file_name,
   max_extent = _xdg_mime_cache_get_max_buffer_extents ();
   data = malloc (max_extent);
   if (data == NULL)
-    return XDG_MIME_TYPE_UNKNOWN;
+    {
+      fclose(file);
+      return XDG_MIME_TYPE_UNKNOWN;
+    }
 
   bytes_read = fread (data, 1, max_extent, file);
   if (ferror (file))
index 65d3c8f..c30aa86 100644 (file)
@@ -238,7 +238,10 @@ mime_type_info_list_reload(mime_type_info_list *mtil)
         * If reconstruction is not needed, just exit function */
        fd = fileno(globs2);
        if (fstat(fd, &globs2_stat) ||
-                       globs2_stat.st_mtime <= mtil->globs2_mtime ) return;
+                       globs2_stat.st_mtime <= mtil->globs2_mtime ) {
+               fclose(globs2);
+               return;
+       }
 
        /* clean old mtil */
        mime_type_info **mti;