gnunet-svn
[Top][All Lists]
Advanced

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

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


From: gnunet
Subject: [GNUnet-SVN] r36832 - libmicrohttpd/src/examples
Date: Mon, 18 Jan 2016 21:45:12 +0100

Author: grothoff
Date: 2016-01-18 21:45:12 +0100 (Mon, 18 Jan 2016)
New Revision: 36832

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

Modified: libmicrohttpd/src/examples/fileserver_example_external_select.c
===================================================================
--- libmicrohttpd/src/examples/fileserver_example_external_select.c     
2016-01-18 20:43:27 UTC (rev 36831)
+++ libmicrohttpd/src/examples/fileserver_example_external_select.c     
2016-01-18 20:45:12 UTC (rev 36832)
@@ -38,6 +38,7 @@
   return fread (buf, 1, max, file);
 }
 
+
 static void
 free_callback (void *cls)
 {
@@ -45,6 +46,7 @@
   fclose (file);
 }
 
+
 static int
 ahc_echo (void *cls,
           struct MHD_Connection *connection,
@@ -58,6 +60,7 @@
   struct MHD_Response *response;
   int ret;
   FILE *file;
+  int fd;
   struct stat buf;
 
   if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
@@ -69,13 +72,24 @@
       return MHD_YES;
     }
   *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 (PAGE),
                                                  (void *) PAGE,
                                                  MHD_RESPMEM_PERSISTENT);
@@ -88,7 +102,7 @@
                                                     &file_reader,
                                                     file,
                                                     &free_callback);
-      if (response == NULL)
+      if (NULL == response)
        {
          fclose (file);
          return MHD_NO;
@@ -99,6 +113,7 @@
   return ret;
 }
 
+
 int
 main (int argc, char *const *argv)
 {




reply via email to

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