gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r36831 - libmicrohttpd/src/examples


From: gnunet
Subject: [GNUnet-SVN] r36831 - libmicrohttpd/src/examples
Date: Mon, 18 Jan 2016 21:43:28 +0100

Author: grothoff
Date: 2016-01-18 21:43:27 +0100 (Mon, 18 Jan 2016)
New Revision: 36831

Modified:
   libmicrohttpd/src/examples/https_fileserver_example.c
Log:
eliminate stat/fopen race in example

Modified: libmicrohttpd/src/examples/https_fileserver_example.c
===================================================================
--- libmicrohttpd/src/examples/https_fileserver_example.c       2016-01-18 
20:37:15 UTC (rev 36830)
+++ libmicrohttpd/src/examples/https_fileserver_example.c       2016-01-18 
20:43:27 UTC (rev 36831)
@@ -125,6 +125,7 @@
   struct MHD_Response *response;
   int ret;
   FILE *file;
+  int fd;
   struct stat buf;
 
   if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
@@ -137,13 +138,23 @@
     }
   *ptr = NULL;                  /* reset when done */
 
-  if ( (0 == stat (&url[1], &buf)) &&
-       (S_ISREG (buf.st_mode)) )
-    file = fopen (&url[1], "rb");
-  else
-    file = NULL;
-  if (file == NULL)
+  file = fopen (&url[1], "rb");
+  if (NULL != file)
     {
+      fd = fileno (file);
+      if (-1 == fd)
+        return MHD_NO; /* internal error */
+      if ( (0 != fstat (fd, &buf)) ||
+           (! S_ISREG (buf.st_mode)) )
+        {
+          /* not a regular file, refuse to serve */
+          fclose (file);
+          file = NULL;
+        }
+    }
+
+  if (NULL == file)
+    {
       response = MHD_create_response_from_buffer (strlen (EMPTY_PAGE),
                                                  (void *) EMPTY_PAGE,
                                                  MHD_RESPMEM_PERSISTENT);
@@ -155,7 +166,7 @@
       response = MHD_create_response_from_callback (buf.st_size, 32 * 1024,    
 /* 32k PAGE_NOT_FOUND size */
                                                     &file_reader, file,
                                                     &file_free_callback);
-      if (response == NULL)
+      if (NULL == response)
        {
          fclose (file);
          return MHD_NO;
@@ -166,6 +177,7 @@
   return ret;
 }
 
+
 int
 main (int argc, char *const *argv)
 {




reply via email to

[Prev in Thread] Current Thread [Next in Thread]