Fix TOCTOU issues
[platform/core/appfw/xdgmime.git] / xdgmime / src / xdgmime.c
index 9e0e5e7..f898cc1 100644 (file)
@@ -533,6 +533,7 @@ xdg_mime_get_mime_type_for_file (const char  *file_name,
   struct stat buf;
   const char *base_name;
   int n;
+  int fd;
 
   if (file_name == NULL)
     return NULL;
@@ -550,9 +551,17 @@ xdg_mime_get_mime_type_for_file (const char  *file_name,
   if (n == 1)
     return mime_types[0];
 
+  file = fopen (file_name, "r");
+  if (file == NULL)
+    {
+      free (data);
+      return XDG_MIME_TYPE_UNKNOWN;
+    }
+
   if (!statbuf)
     {
-      if (stat (file_name, &buf) != 0)
+      fd = fileno(file);
+      if (fstat (fd, &buf) != 0)
        return XDG_MIME_TYPE_UNKNOWN;
 
       statbuf = &buf;
@@ -569,13 +578,6 @@ xdg_mime_get_mime_type_for_file (const char  *file_name,
   if (data == NULL)
     return XDG_MIME_TYPE_UNKNOWN;
         
-  file = fopen (file_name, "r");
-  if (file == NULL)
-    {
-      free (data);
-      return XDG_MIME_TYPE_UNKNOWN;
-    }
-
   bytes_read = fread (data, 1, max_extent, file);
   if (ferror (file))
     {