gnunet-svn
[Top][All Lists]
Advanced

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

[libmicrohttpd] branch master updated (1b8c52f1 -> badf7560)


From: gnunet
Subject: [libmicrohttpd] branch master updated (1b8c52f1 -> badf7560)
Date: Wed, 01 Jun 2022 21:13:28 +0200

This is an automated email from the git hooks/post-receive script.

karlson2k pushed a change to branch master
in repository libmicrohttpd.

    from 1b8c52f1 daemon: muted compiler warning, refactored code for 
readability
     new 74c2eba2 connection: muted compiler warning
     new 93dcf8ea src/examples: muted compiler warnings
     new 73340d1d microhttpd.h: fixed doxy
     new aefa12e9 src/examples/*fileserver*.c: added error checking
     new 0e829bf2 src/examples/benchmark{,_https}: simplified time calculation
     new 5821a7f3 src/examples/demo{,_https}: added some error checking, fixed 
compiler warnings
     new badf7560 http_chunked_compression: fixed errors

The 7 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 doc/examples/sessions.c                           |   4 +-
 src/examples/authorization_example.c              |   4 +-
 src/examples/benchmark.c                          |  19 ++--
 src/examples/benchmark_https.c                    |  19 ++--
 src/examples/chunked_example.c                    |   6 +-
 src/examples/demo.c                               | 111 ++++++++++++++-------
 src/examples/demo_https.c                         | 113 +++++++++++++++-------
 src/examples/digest_auth_example.c                |  18 ++--
 src/examples/dual_stack_example.c                 |  10 +-
 src/examples/fileserver_example.c                 |  13 ++-
 src/examples/fileserver_example_dirs.c            |  46 +++++++--
 src/examples/fileserver_example_external_select.c |  45 +++++++--
 src/examples/http_chunked_compression.c           |  30 +++---
 src/examples/http_compression.c                   |  13 ++-
 src/examples/https_fileserver_example.c           |  18 +++-
 src/examples/minimal_example.c                    |  10 +-
 src/examples/minimal_example_comet.c              |  12 ++-
 src/examples/minimal_example_empty.c              |  12 ++-
 src/examples/minimal_example_empty_tls.c          |  14 ++-
 src/examples/post_example.c                       |  20 +++-
 src/examples/querystring_example.c                |  12 ++-
 src/examples/refuse_post_example.c                |  24 +++--
 src/examples/suspend_resume_epoll.c               |  10 +-
 src/examples/upgrade_example.c                    |  17 +++-
 src/include/microhttpd.h                          |   3 +-
 src/microhttpd/connection.c                       |  16 ++-
 26 files changed, 448 insertions(+), 171 deletions(-)

diff --git a/doc/examples/sessions.c b/doc/examples/sessions.c
index 121cf2e0..7f62e73b 100644
--- a/doc/examples/sessions.c
+++ b/doc/examples/sessions.c
@@ -778,8 +778,8 @@ main (int argc, char *const *argv)
       break; /* fatal internal error */
     if (MHD_get_timeout64 (d, &mhd_timeout) == MHD_YES)
     {
-      tv.tv_sec = mhd_timeout / 1000;
-      tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000)) * 1000;
+      tv.tv_sec = (time_t) mhd_timeout / 1000;
+      tv.tv_usec = ((long) (mhd_timeout % 1000)) * 1000;
       tvp = &tv;
     }
     else
diff --git a/src/examples/authorization_example.c 
b/src/examples/authorization_example.c
index d5011e5d..8d26869a 100644
--- a/src/examples/authorization_example.c
+++ b/src/examples/authorization_example.c
@@ -109,7 +109,7 @@ main (int argc, char *const *argv)
 
   if ( (argc != 2) ||
        (1 != sscanf (argv[1], "%u", &port)) ||
-       (UINT16_MAX < port) )
+       (65535 < port) )
   {
     fprintf (stderr,
              "%s PORT\n", argv[0]);
@@ -118,7 +118,7 @@ main (int argc, char *const *argv)
 
   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
-                        atoi (argv[1]),
+                        (uint16_t) port,
                         NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
   if (d == NULL)
     return 1;
diff --git a/src/examples/benchmark.c b/src/examples/benchmark.c
index 37e1b4f8..1e8361a3 100644
--- a/src/examples/benchmark.c
+++ b/src/examples/benchmark.c
@@ -86,13 +86,8 @@ completed_callback (void *cls,
     return;
   gettimeofday (&tve, NULL);
 
-  delta = 0;
-  if (tve.tv_usec >= tv->tv_usec)
-    delta += (tve.tv_sec - tv->tv_sec) * 1000000LL
-             + (tve.tv_usec - tv->tv_usec);
-  else
-    delta += (tve.tv_sec - tv->tv_sec) * 1000000LL
-             - tv->tv_usec + tve.tv_usec;
+  delta = ((uint64_t) (tve.tv_sec - tv->tv_sec)) * 1000000LL
+          + (uint64_t) tve.tv_usec - (uint64_t) tv->tv_usec;
   if (delta < SMALL)
     small_deltas[delta]++;
   else
@@ -141,12 +136,20 @@ main (int argc, char *const *argv)
 {
   struct MHD_Daemon *d;
   unsigned int i;
+  int port;
 
   if (argc != 2)
   {
     printf ("%s PORT\n", argv[0]);
     return 1;
   }
+  port = atoi (argv[1]);
+  if ( (1 > port) || (port > 65535) )
+  {
+    fprintf (stderr,
+             "Port must be a number between 1 and 65535.\n");
+    return 1;
+  }
   response = MHD_create_response_from_buffer_static (strlen (PAGE),
                                                      (const void *) PAGE);
 #if 0
@@ -160,7 +163,7 @@ main (int argc, char *const *argv)
                         | MHD_USE_EPOLL | MHD_USE_TURBO
 #endif
                         ,
-                        atoi (argv[1]),
+                        (uint16_t) port,
                         NULL, NULL, &ahc_echo, NULL,
                         MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
                         MHD_OPTION_THREAD_POOL_SIZE, (unsigned
diff --git a/src/examples/benchmark_https.c b/src/examples/benchmark_https.c
index 9553d434..7f391b0e 100644
--- a/src/examples/benchmark_https.c
+++ b/src/examples/benchmark_https.c
@@ -86,13 +86,8 @@ completed_callback (void *cls,
     return;
   gettimeofday (&tve, NULL);
 
-  delta = 0;
-  if (tve.tv_usec >= tv->tv_usec)
-    delta += (tve.tv_sec - tv->tv_sec) * 1000000LL
-             + (tve.tv_usec - tv->tv_usec);
-  else
-    delta += (tve.tv_sec - tv->tv_sec) * 1000000LL
-             - tv->tv_usec + tve.tv_usec;
+  delta = ((uint64_t) (tve.tv_sec - tv->tv_sec)) * 1000000LL
+          + (uint64_t) tve.tv_usec - (uint64_t) tv->tv_usec;
   if (delta < SMALL)
     small_deltas[delta]++;
   else
@@ -207,12 +202,20 @@ main (int argc, char *const *argv)
 {
   struct MHD_Daemon *d;
   unsigned int i;
+  int port;
 
   if (argc != 2)
   {
     printf ("%s PORT\n", argv[0]);
     return 1;
   }
+  port = atoi (argv[1]);
+  if ( (1 > port) || (port > 65535) )
+  {
+    fprintf (stderr,
+             "Port must be a number between 1 and 65535.\n");
+    return 1;
+  }
   response = MHD_create_response_from_buffer_static (strlen (PAGE),
                                                      (const void *) PAGE);
   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS
@@ -220,7 +223,7 @@ main (int argc, char *const *argv)
                         | MHD_USE_EPOLL | MHD_USE_TURBO
 #endif
                         ,
-                        atoi (argv[1]),
+                        (uint16_t) port,
                         NULL, NULL, &ahc_echo, NULL,
                         MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
                         MHD_OPTION_THREAD_POOL_SIZE, (unsigned
diff --git a/src/examples/chunked_example.c b/src/examples/chunked_example.c
index 5a4c0ced..12d07d90 100644
--- a/src/examples/chunked_example.c
+++ b/src/examples/chunked_example.c
@@ -1,6 +1,7 @@
 /*
      This file is part of libmicrohttpd
      Copyright (C) 2015 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2016-2022 Evgeny Grin (Karlson2k)
 
      This library is free software; you can redistribute it and/or
      modify it under the terms of the GNU Lesser General Public
@@ -73,7 +74,10 @@ callback (void *cls,
     }
    * End of pseudo code. */
   /* Return amount of data copied to buffer. */
-  return size_to_copy;
+  /* The 'buf_size' is always smaller than SSIZE_MAX therefore it's safe
+   * to cast 'size_to_copy' to 'ssize_t'. */
+  /* assert (size_to_copy <= buf_size); */
+  return (ssize_t) size_to_copy;
 }
 
 
diff --git a/src/examples/demo.c b/src/examples/demo.c
index d2dc15e8..0ab7a973 100644
--- a/src/examples/demo.c
+++ b/src/examples/demo.c
@@ -280,6 +280,7 @@ list_directory (struct ResponseDataContext *rdc,
     return MHD_NO;
   while (NULL != (de = readdir (dir)))
   {
+    int res;
     if ('.' == de->d_name[0])
       continue;
     if (sizeof (fullname) <= (unsigned int)
@@ -302,11 +303,16 @@ list_directory (struct ResponseDataContext *rdc,
         break; /* out of memory */
       rdc->buf = r;
     }
-    rdc->off += snprintf (&rdc->buf[rdc->off],
-                          rdc->buf_len - rdc->off,
-                          "<li><a href=\"/%s\">%s</a></li>\n",
-                          fullname,
-                          de->d_name);
+    res = snprintf (&rdc->buf[rdc->off],
+                    rdc->buf_len - rdc->off,
+                    "<li><a href=\"/%s\">%s</a></li>\n",
+                    fullname,
+                    de->d_name);
+    if (0 >= res)
+      continue;  /* snprintf() error */
+    if (rdc->buf_len - rdc->off <= (size_t) res)
+      continue;  /* buffer too small?? */
+    rdc->off += (size_t) res;
   }
   (void) closedir (dir);
   return MHD_YES;
@@ -328,6 +334,8 @@ update_directory (void)
   const char *category;
   char dir_name[128];
   struct stat sbuf;
+  int res;
+  size_t len;
 
   rdc.buf_len = initial_allocation;
   if (NULL == (rdc.buf = malloc (rdc.buf_len)))
@@ -335,9 +343,15 @@ update_directory (void)
     update_cached_response (NULL);
     return;
   }
-  rdc.off = snprintf (rdc.buf, rdc.buf_len,
-                      "%s",
-                      INDEX_PAGE_HEADER);
+  len = strlen (INDEX_PAGE_HEADER);
+  if (rdc.buf_len <= len)
+  { /* buffer too small */
+    free (rdc.buf);
+    update_cached_response (NULL);
+    return;
+  }
+  memcpy (rdc.buf, INDEX_PAGE_HEADER, len);
+  rdc.off = len;
   for (language_idx = 0; NULL != languages[language_idx].dirname;
        language_idx++)
   {
@@ -346,27 +360,39 @@ update_directory (void)
     if (0 != stat (language->dirname, &sbuf))
       continue; /* empty */
     /* we ensured always +1k room, filenames are ~256 bytes,
- so there is always still enough space for the header
- without need for an additional reallocation check. */
-    rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
-                         "<h2>%s</h2>\n",
-                         language->longname);
+       so there is always still enough space for the header
+       without need for an additional reallocation check. */
+    res = snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
+                    "<h2>%s</h2>\n",
+                    language->longname);
+    if (0 >= res)
+      continue;  /* snprintf() error */
+    if (rdc.buf_len - rdc.off <= (size_t) res)
+      continue;  /* buffer too small?? */
+    rdc.off += (size_t) res;
     for (category_idx = 0; NULL != categories[category_idx]; category_idx++)
     {
       category = categories[category_idx];
-      snprintf (dir_name, sizeof (dir_name),
-                "%s/%s",
-                language->dirname,
-                category);
+      res = snprintf (dir_name, sizeof (dir_name),
+                      "%s/%s",
+                      language->dirname,
+                      category);
+      if ((0 >= res) || (sizeof (dir_name) <= (size_t) res))
+        continue;  /* cannot print dir name */
       if (0 != stat (dir_name, &sbuf))
-        continue; /* empty */
+        continue;  /* empty */
 
       /* we ensured always +1k room, filenames are ~256 bytes,
          so there is always still enough space for the header
          without need for an additional reallocation check. */
-      rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
-                           "<h3>%s</h3>\n",
-                           category);
+      res = snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
+                      "<h3>%s</h3>\n",
+                      category);
+      if (0 >= res)
+        continue;  /* snprintf() error */
+      if (rdc.buf_len - rdc.off <= (size_t) res)
+        continue;  /* buffer too small?? */
+      rdc.off += (size_t) res;
 
       if (MHD_NO == list_directory (&rdc, dir_name))
       {
@@ -379,9 +405,15 @@ update_directory (void)
   /* we ensured always +1k room, filenames are ~256 bytes,
      so there is always still enough space for the footer
      without need for a final reallocation check. */
-  rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
-                       "%s",
-                       INDEX_PAGE_FOOTER);
+  len = strlen (INDEX_PAGE_FOOTER);
+  if (rdc.buf_len - rdc.off <= len)
+  { /* buffer too small */
+    free (rdc.buf);
+    update_cached_response (NULL);
+    return;
+  }
+  memcpy (rdc.buf, INDEX_PAGE_FOOTER, len);
+  rdc.off += len;
   initial_allocation = rdc.buf_len; /* remember for next time */
   response =
     MHD_create_response_from_buffer_with_free_callback (rdc.off,
@@ -509,7 +541,8 @@ process_upload_data (void *cls,
                      size_t size)
 {
   struct UploadContext *uc = cls;
-  int i;
+  size_t i;
+  int res;
   (void) kind;              /* Unused. Silent compiler warning. */
   (void) content_type;      /* Unused. Silent compiler warning. */
   (void) transfer_encoding; /* Unused. Silent compiler warning. */
@@ -567,12 +600,17 @@ process_upload_data (void *cls,
     (void) mkdir (fn, S_IRWXU);
 #endif
     /* open file */
-    snprintf (fn, sizeof (fn),
-              "%s/%s/%s",
-              uc->language,
-              uc->category,
-              filename);
-    for (i = strlen (fn) - 1; i >= 0; i--)
+    res = snprintf (fn, sizeof (fn),
+                    "%s/%s/%s",
+                    uc->language,
+                    uc->category,
+                    filename);
+    if ((0 >= res) || (sizeof (fn) <= (size_t) res))
+    {
+      uc->response = request_refused_response;
+      return MHD_NO;
+    }
+    for (i = 0; i < (size_t) res; i++)
       if (! isprint ((unsigned char) fn[i]))
         fn[i] = '_';
     uc->fd = open (fn,
@@ -594,7 +632,12 @@ process_upload_data (void *cls,
     uc->filename = strdup (fn);
   }
   if ( (0 != size) &&
-       (size != (size_t) write (uc->fd, data, size)) )
+#if ! defined(_WIN32) || defined(__CYGWIN__)
+       (size != (size_t) write (uc->fd, data, size))
+#else  /* Native W32 */
+       (size != (size_t) write (uc->fd, data, (unsigned int) size))
+#endif /* Native W32 */
+       )
   {
     /* write failed; likely: disk full */
     fprintf (stderr,
@@ -755,7 +798,7 @@ generate_page (void *cls,
 #endif /* MHD_HAVE_LIBMAGIC */
     mime = NULL;
 
-    if (NULL == (response = MHD_create_response_from_fd (buf.st_size,
+    if (NULL == (response = MHD_create_response_from_fd ((size_t) buf.st_size,
                                                          fd)))
     {
       /* internal error (i.e. out of memory) */
@@ -928,7 +971,7 @@ main (int argc, char *const *argv)
   update_directory ();
   d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD
                         | MHD_USE_ERROR_LOG,
-                        port,
+                        (uint16_t) port,
                         NULL, NULL,
                         &generate_page, NULL,
                         MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (256
diff --git a/src/examples/demo_https.c b/src/examples/demo_https.c
index dabb0205..940bf556 100644
--- a/src/examples/demo_https.c
+++ b/src/examples/demo_https.c
@@ -282,6 +282,7 @@ list_directory (struct ResponseDataContext *rdc,
     return MHD_NO;
   while (NULL != (de = readdir (dir)))
   {
+    int res;
     if ('.' == de->d_name[0])
       continue;
     if (sizeof (fullname) <= (size_t)
@@ -304,11 +305,16 @@ list_directory (struct ResponseDataContext *rdc,
         break; /* out of memory */
       rdc->buf = r;
     }
-    rdc->off += snprintf (&rdc->buf[rdc->off],
-                          rdc->buf_len - rdc->off,
-                          "<li><a href=\"/%s\">%s</a></li>\n",
-                          fullname,
-                          de->d_name);
+    res = snprintf (&rdc->buf[rdc->off],
+                    rdc->buf_len - rdc->off,
+                    "<li><a href=\"/%s\">%s</a></li>\n",
+                    fullname,
+                    de->d_name);
+    if (0 >= res)
+      continue;  /* snprintf() error */
+    if (rdc->buf_len - rdc->off <= (size_t) res)
+      continue;  /* buffer too small?? */
+    rdc->off += (size_t) res;
   }
   (void) closedir (dir);
   return MHD_YES;
@@ -330,6 +336,8 @@ update_directory (void)
   const char *category;
   char dir_name[128];
   struct stat sbuf;
+  int res;
+  size_t len;
 
   rdc.buf_len = initial_allocation;
   if (NULL == (rdc.buf = malloc (rdc.buf_len)))
@@ -337,9 +345,15 @@ update_directory (void)
     update_cached_response (NULL);
     return;
   }
-  rdc.off = snprintf (rdc.buf, rdc.buf_len,
-                      "%s",
-                      INDEX_PAGE_HEADER);
+  len = strlen (INDEX_PAGE_HEADER);
+  if (rdc.buf_len <= len)
+  { /* buffer too small */
+    free (rdc.buf);
+    update_cached_response (NULL);
+    return;
+  }
+  memcpy (rdc.buf, INDEX_PAGE_HEADER, len);
+  rdc.off = len;
   for (language_idx = 0; NULL != languages[language_idx].dirname;
        language_idx++)
   {
@@ -348,27 +362,39 @@ update_directory (void)
     if (0 != stat (language->dirname, &sbuf))
       continue; /* empty */
     /* we ensured always +1k room, filenames are ~256 bytes,
- so there is always still enough space for the header
- without need for an additional reallocation check. */
-    rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
-                         "<h2>%s</h2>\n",
-                         language->longname);
+       so there is always still enough space for the header
+       without need for an additional reallocation check. */
+    res = snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
+                    "<h2>%s</h2>\n",
+                    language->longname);
+    if (0 >= res)
+      continue;  /* snprintf() error */
+    if (rdc.buf_len - rdc.off <= (size_t) res)
+      continue;  /* buffer too small?? */
+    rdc.off += (size_t) res;
     for (category_idx = 0; NULL != categories[category_idx]; category_idx++)
     {
       category = categories[category_idx];
-      snprintf (dir_name, sizeof (dir_name),
-                "%s/%s",
-                language->dirname,
-                category);
+      res = snprintf (dir_name, sizeof (dir_name),
+                      "%s/%s",
+                      language->dirname,
+                      category);
+      if ((0 >= res) || (sizeof (dir_name) <= (size_t) res))
+        continue;  /* cannot print dir name */
       if (0 != stat (dir_name, &sbuf))
-        continue; /* empty */
+        continue;  /* empty */
 
       /* we ensured always +1k room, filenames are ~256 bytes,
          so there is always still enough space for the header
          without need for an additional reallocation check. */
-      rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
-                           "<h3>%s</h3>\n",
-                           category);
+      res = snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
+                      "<h3>%s</h3>\n",
+                      category);
+      if (0 >= res)
+        continue;  /* snprintf() error */
+      if (rdc.buf_len - rdc.off <= (size_t) res)
+        continue;  /* buffer too small?? */
+      rdc.off += (size_t) res;
 
       if (MHD_NO == list_directory (&rdc, dir_name))
       {
@@ -381,9 +407,15 @@ update_directory (void)
   /* we ensured always +1k room, filenames are ~256 bytes,
      so there is always still enough space for the footer
      without need for a final reallocation check. */
-  rdc.off += snprintf (&rdc.buf[rdc.off], rdc.buf_len - rdc.off,
-                       "%s",
-                       INDEX_PAGE_FOOTER);
+  len = strlen (INDEX_PAGE_FOOTER);
+  if (rdc.buf_len - rdc.off <= len)
+  { /* buffer too small */
+    free (rdc.buf);
+    update_cached_response (NULL);
+    return;
+  }
+  memcpy (rdc.buf, INDEX_PAGE_FOOTER, len);
+  rdc.off += len;
   initial_allocation = rdc.buf_len; /* remember for next time */
   response =
     MHD_create_response_from_buffer_with_free_callback (rdc.off,
@@ -511,7 +543,8 @@ process_upload_data (void *cls,
                      size_t size)
 {
   struct UploadContext *uc = cls;
-  int i;
+  size_t i;
+  int res;
   (void) kind;              /* Unused. Silent compiler warning. */
   (void) content_type;      /* Unused. Silent compiler warning. */
   (void) transfer_encoding; /* Unused. Silent compiler warning. */
@@ -569,12 +602,17 @@ process_upload_data (void *cls,
     (void) mkdir (fn, S_IRWXU);
 #endif
     /* open file */
-    snprintf (fn, sizeof (fn),
-              "%s/%s/%s",
-              uc->language,
-              uc->category,
-              filename);
-    for (i = strlen (fn) - 1; i >= 0; i--)
+    res = snprintf (fn, sizeof (fn),
+                    "%s/%s/%s",
+                    uc->language,
+                    uc->category,
+                    filename);
+    if ((0 >= res) || (sizeof (fn) <= (size_t) res))
+    {
+      uc->response = request_refused_response;
+      return MHD_NO;
+    }
+    for (i = 0; i < (size_t) res; i++)
       if (! isprint ((unsigned char) fn[i]))
         fn[i] = '_';
     uc->fd = open (fn,
@@ -596,7 +634,12 @@ process_upload_data (void *cls,
     uc->filename = strdup (fn);
   }
   if ( (0 != size) &&
-       (size != (size_t) write (uc->fd, data, size)) )
+#if ! defined(_WIN32) || defined(__CYGWIN__)
+       (size != (size_t) write (uc->fd, data, size))
+#else  /* Native W32 */
+       (size != (size_t) write (uc->fd, data, (unsigned int) size))
+#endif /* Native W32 */
+       )
   {
     /* write failed; likely: disk full */
     fprintf (stderr,
@@ -604,7 +647,7 @@ process_upload_data (void *cls,
              uc->filename,
              strerror (errno));
     uc->response = internal_error_response;
-    close (uc->fd);
+    (void) close (uc->fd);
     uc->fd = -1;
     if (NULL != uc->filename)
     {
@@ -756,7 +799,7 @@ generate_page (void *cls,
 #endif /* MHD_HAVE_LIBMAGIC */
     mime = NULL;
 
-    if (NULL == (response = MHD_create_response_from_fd (buf.st_size,
+    if (NULL == (response = MHD_create_response_from_fd ((size_t) buf.st_size,
                                                          fd)))
     {
       /* internal error (i.e. out of memory) */
@@ -993,7 +1036,7 @@ main (int argc, char *const *argv)
   update_directory ();
   d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD
                         | MHD_USE_ERROR_LOG | MHD_USE_TLS,
-                        port,
+                        (uint16_t) port,
                         NULL, NULL,
                         &generate_page, NULL,
                         MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (256
diff --git a/src/examples/digest_auth_example.c 
b/src/examples/digest_auth_example.c
index b3741bc5..4f576bf0 100644
--- a/src/examples/digest_auth_example.c
+++ b/src/examples/digest_auth_example.c
@@ -29,10 +29,12 @@
 #include <stdlib.h>
 
 #define PAGE \
-  "<html><head><title>libmicrohttpd demo</title></head><body>Access 
granted</body></html>"
+  "<html><head><title>libmicrohttpd demo</title></head>" \
+  "<body>Access granted</body></html>"
 
 #define DENIED \
-  "<html><head><title>libmicrohttpd demo</title></head><body>Access 
denied</body></html>"
+  "<html><head><title>libmicrohttpd demo</title></head>" \
+  "<body>Access denied</body></html>"
 
 #define MY_OPAQUE_STR "11733b200778ce33060f31c9af70a870ba96ddd4"
 
@@ -116,12 +118,16 @@ main (int argc, char *const *argv)
   ssize_t len;
   size_t off;
   struct MHD_Daemon *d;
+  unsigned int port;
 
-  if (argc != 2)
+  if ( (argc != 2) ||
+       (1 != sscanf (argv[1], "%u", &port)) ||
+       (65535 < port) )
   {
     printf ("%s PORT\n", argv[0]);
     return 1;
   }
+
   fd = open ("/dev/urandom", O_RDONLY);
   if (-1 == fd)
   {
@@ -134,7 +140,7 @@ main (int argc, char *const *argv)
   while (off < 8)
   {
     len = read (fd, rnd, 8);
-    if (len == -1)
+    if (0 > len)
     {
       fprintf (stderr, "Failed to read `%s': %s\n",
                "/dev/urandom",
@@ -142,12 +148,12 @@ main (int argc, char *const *argv)
       (void) close (fd);
       return 1;
     }
-    off += len;
+    off += (size_t) len;
   }
   (void) close (fd);
   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
-                        atoi (argv[1]),
+                        (uint16_t) port,
                         NULL, NULL, &ahc_echo, NULL,
                         MHD_OPTION_DIGEST_AUTH_RANDOM, sizeof(rnd), rnd,
                         MHD_OPTION_NONCE_NC_SIZE, 300,
diff --git a/src/examples/dual_stack_example.c 
b/src/examples/dual_stack_example.c
index 957daf07..57a3b558 100644
--- a/src/examples/dual_stack_example.c
+++ b/src/examples/dual_stack_example.c
@@ -75,16 +75,24 @@ main (int argc, char *const *argv)
 {
   struct MHD_Daemon *d;
   struct handler_param data_for_handler;
+  int port;
 
   if (argc != 2)
   {
     printf ("%s PORT\n", argv[0]);
     return 1;
   }
+  port = atoi (argv[1]);
+  if ( (1 > port) || (port > 65535) )
+  {
+    fprintf (stderr,
+             "Port must be a number between 1 and 65535.\n");
+    return 1;
+  }
   data_for_handler.response_page = PAGE;
   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
                         | MHD_USE_DUAL_STACK,
-                        atoi (argv[1]),
+                        (uint16_t) port,
                         NULL, NULL, &ahc_echo, &data_for_handler,
                         MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
                         MHD_OPTION_END);
diff --git a/src/examples/fileserver_example.c 
b/src/examples/fileserver_example.c
index e300f6ed..ee80d632 100644
--- a/src/examples/fileserver_example.c
+++ b/src/examples/fileserver_example.c
@@ -100,7 +100,7 @@ ahc_echo (void *cls,
   }
   else
   {
-    response = MHD_create_response_from_fd64 (buf.st_size, fd);
+    response = MHD_create_response_from_fd64 ((uint64_t) buf.st_size, fd);
     if (NULL == response)
     {
       if (0 != close (fd))
@@ -118,15 +118,24 @@ int
 main (int argc, char *const *argv)
 {
   struct MHD_Daemon *d;
+  int port;
 
   if (argc != 2)
   {
     printf ("%s PORT\n", argv[0]);
     return 1;
   }
+  port = atoi (argv[1]);
+  if ( (1 > port) || (port > 65535) )
+  {
+    fprintf (stderr,
+             "Port must be a number between 1 and 65535.\n");
+    return 1;
+  }
+
   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
-                        atoi (argv[1]),
+                        (uint16_t) port,
                         NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
   if (d == NULL)
     return 1;
diff --git a/src/examples/fileserver_example_dirs.c 
b/src/examples/fileserver_example_dirs.c
index 0a977ddf..5ec60428 100644
--- a/src/examples/fileserver_example_dirs.c
+++ b/src/examples/fileserver_example_dirs.c
@@ -28,16 +28,27 @@
 #include "platform.h"
 #include <dirent.h>
 #include <microhttpd.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
 
 
 static ssize_t
 file_reader (void *cls, uint64_t pos, char *buf, size_t max)
 {
-  FILE *file = cls;
-
-  (void) fseek (file, pos, SEEK_SET);
-  return fread (buf, 1, max, file);
+  FILE *file = (FILE *) cls;
+  size_t bytes_read;
+
+  /* 'fseek' may not support files larger 2GiB, depending on platform.
+   * For production code, make sure that 'pos' has valid values, supported by
+   * 'fseek', or use 'fseeko' or similar function. */
+  if (0 != fseek (file, (long) pos, SEEK_SET))
+    return MHD_CONTENT_READER_END_WITH_ERROR;
+  bytes_read = fread (buf, 1, max, file);
+  if (0 == bytes_read)
+    return (0 != ferror (file)) ? MHD_CONTENT_READER_END_WITH_ERROR :
+           MHD_CONTENT_READER_END_OF_STREAM;
+  return (ssize_t) bytes_read;
 }
 
 
@@ -63,6 +74,7 @@ dir_reader (void *cls, uint64_t pos, char *buf, size_t max)
 {
   DIR *dir = cls;
   struct dirent *e;
+  int res;
 
   if (max < 512)
     return 0;
@@ -73,10 +85,15 @@ dir_reader (void *cls, uint64_t pos, char *buf, size_t max)
     if (e == NULL)
       return MHD_CONTENT_READER_END_OF_STREAM;
   } while (e->d_name[0] == '.');
-  return snprintf (buf, max,
-                   "<a href=\"/%s\">%s</a><br>",
-                   e->d_name,
-                   e->d_name);
+  res = snprintf (buf, max,
+                  "<a href=\"/%s\">%s</a><br>",
+                  e->d_name,
+                  e->d_name);
+  if (0 >= res)
+    return MHD_CONTENT_READER_END_WITH_ERROR;
+  if (max < (size_t) res)
+    return MHD_CONTENT_READER_END_WITH_ERROR;
+  return (ssize_t) res;
 }
 
 
@@ -169,7 +186,8 @@ ahc_echo (void *cls,
   }
   else
   {
-    response = MHD_create_response_from_callback (buf.st_size, 32 * 1024,      
 /* 32k page size */
+    response = MHD_create_response_from_callback ((size_t) buf.st_size,
+                                                  32 * 1024, /* 32k page size 
*/
                                                   &file_reader,
                                                   file,
                                                   &file_free_callback);
@@ -189,15 +207,23 @@ int
 main (int argc, char *const *argv)
 {
   struct MHD_Daemon *d;
+  int port;
 
   if (argc != 2)
   {
     printf ("%s PORT\n", argv[0]);
     return 1;
   }
+  port = atoi (argv[1]);
+  if ( (1 > port) || (port > 65535) )
+  {
+    fprintf (stderr,
+             "Port must be a number between 1 and 65535.\n");
+    return 1;
+  }
   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
-                        atoi (argv[1]),
+                        (uint16_t) port,
                         NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
   if (NULL == d)
     return 1;
diff --git a/src/examples/fileserver_example_external_select.c 
b/src/examples/fileserver_example_external_select.c
index 3c077413..192e00d1 100644
--- a/src/examples/fileserver_example_external_select.c
+++ b/src/examples/fileserver_example_external_select.c
@@ -35,10 +35,19 @@
 static ssize_t
 file_reader (void *cls, uint64_t pos, char *buf, size_t max)
 {
-  FILE *file = cls;
-
-  (void) fseek (file, pos, SEEK_SET);
-  return fread (buf, 1, max, file);
+  FILE *file = (FILE *) cls;
+  size_t bytes_read;
+
+  /* 'fseek' may not support files larger 2GiB, depending on platform.
+   * For production code, make sure that 'pos' has valid values, supported by
+   * 'fseek', or use 'fseeko' or similar function. */
+  if (0 != fseek (file, (long) pos, SEEK_SET))
+    return MHD_CONTENT_READER_END_WITH_ERROR;
+  bytes_read = fread (buf, 1, max, file);
+  if (0 == bytes_read)
+    return (0 != ferror (file)) ? MHD_CONTENT_READER_END_WITH_ERROR :
+           MHD_CONTENT_READER_END_OF_STREAM;
+  return (ssize_t) bytes_read;
 }
 
 
@@ -107,7 +116,8 @@ ahc_echo (void *cls,
   }
   else
   {
-    response = MHD_create_response_from_callback (buf.st_size, 32 * 1024,      
 /* 32k page size */
+    response = MHD_create_response_from_callback ((size_t) buf.st_size,
+                                                  32 * 1024, /* 32k page size 
*/
                                                   &file_reader,
                                                   file,
                                                   &free_callback);
@@ -135,21 +145,34 @@ main (int argc, char *const *argv)
   fd_set es;
   MHD_socket max;
   uint64_t mhd_timeout;
+  int port;
 
   if (argc != 3)
   {
     printf ("%s PORT SECONDS-TO-RUN\n", argv[0]);
     return 1;
   }
+  port = atoi (argv[1]);
+  if ( (1 > port) || (port > 65535) )
+  {
+    fprintf (stderr,
+             "Port must be a number between 1 and 65535.\n");
+    return 1;
+  }
+
   d = MHD_start_daemon (MHD_USE_ERROR_LOG,
-                        atoi (argv[1]),
+                        (uint16_t) port,
                         NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
   if (d == NULL)
     return 1;
   end = time (NULL) + atoi (argv[2]);
   while ((t = time (NULL)) < end)
   {
+#if ! defined(_WIN32) || defined(__CYGWIN__)
     tv.tv_sec = end - t;
+#else  /* Native W32 */
+    tv.tv_sec = (long) (end - t);
+#endif /* Native W32 */
     tv.tv_usec = 0;
     max = 0;
     FD_ZERO (&rs);
@@ -161,11 +184,15 @@ main (int argc, char *const *argv)
     {
       if (((uint64_t) tv.tv_sec) < mhd_timeout / 1000LL)
       {
-        tv.tv_sec = mhd_timeout / 1000LL;
-        tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000LL)) * 1000LL;
+#if ! defined(_WIN32) || defined(__CYGWIN__)
+        tv.tv_sec = (time_t) (mhd_timeout / 1000LL);
+#else  /* Native W32 */
+        tv.tv_sec = (long) (mhd_timeout / 1000LL);
+#endif /* Native W32 */
+        tv.tv_usec = ((long) (mhd_timeout % 1000)) * 1000;
       }
     }
-    if (-1 == select (max + 1, &rs, &ws, &es, &tv))
+    if (-1 == select ((int) max + 1, &rs, &ws, &es, &tv))
     {
       if (EINTR != errno)
         abort ();
diff --git a/src/examples/http_chunked_compression.c 
b/src/examples/http_chunked_compression.c
index 7b8ed1fc..aa7bd2e5 100644
--- a/src/examples/http_chunked_compression.c
+++ b/src/examples/http_chunked_compression.c
@@ -1,6 +1,7 @@
 /*
      This file is part of libmicrohttpd
      Copyright (C) 2019 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2019-2022 Evgeny Grin (Karlson2k)
 
      This library is free software; you can redistribute it and/or
      modify it under the terms of the GNU Lesser General Public
@@ -20,15 +21,21 @@
  * @file http_chunked_compression.c
  * @brief example for how to compress a chunked HTTP response
  * @author Silvio Clecio (silvioprog)
+ * @author Karlson2k (Evgeny Grin)
  */
 
 #include "platform.h"
+#ifndef ZLIB_CONST
+/* Correct API with const pointer for input data is required */
+#define ZLIB_CONST 1
+#endif /* ! ZLIB_CONST */
 #include <zlib.h>
 #include <microhttpd.h>
 #ifdef HAVE_LIMITS_H
 #include <limits.h>
 #endif /* HAVE_LIMITS_H */
 #include <stddef.h>
+#include <stdint.h>
 
 #ifndef SSIZE_MAX
 #ifdef __SSIZE_MAX__
@@ -76,7 +83,7 @@ compress_buf (z_stream *strm, const void *src, size_t 
src_size, size_t *offset,
       flush = Z_SYNC_FLUSH;
     }
     *offset += strm->avail_in;
-    strm->next_in = (Bytef *) src;
+    strm->next_in = (const Bytef *) src;
     do
     {
       strm->avail_out = CHUNK;
@@ -92,7 +99,7 @@ compress_buf (z_stream *strm, const void *src, size_t 
src_size, size_t *offset,
         return MHD_NO;
       }
       *dest = tmp_dest;
-      memcpy ((*dest) + ((*dest_size) - have), tmp, have);
+      memcpy (((uint8_t *) (*dest)) + ((*dest_size) - have), tmp, have);
     }
     while (0 == strm->avail_out);
   }
@@ -109,6 +116,7 @@ read_cb (void *cls, uint64_t pos, char *mem, size_t size)
   void *buf;
   ssize_t ret;
   size_t offset;
+  size_t r_size;
 
   if (pos > SSIZE_MAX)
     return MHD_CONTENT_READER_END_WITH_ERROR;
@@ -116,24 +124,20 @@ read_cb (void *cls, uint64_t pos, char *mem, size_t size)
   src = malloc (size);
   if (NULL == src)
     return MHD_CONTENT_READER_END_WITH_ERROR;
-  ret = fread (src, 1, size, holder->file);
-  if (ret < 0)
+  r_size = fread (src, 1, size, holder->file);
+  if (0 == r_size)
   {
-    ret = MHD_CONTENT_READER_END_WITH_ERROR;
-    goto done;
-  }
-  if (0 == ret)
-  {
-    ret = MHD_CONTENT_READER_END_OF_STREAM;
+    ret = (0 != ferror (holder->file)) ?
+          MHD_CONTENT_READER_END_WITH_ERROR : MHD_CONTENT_READER_END_OF_STREAM;
     goto done;
   }
-  if (MHD_YES != compress_buf (&holder->stream, src, ret, &offset, &buf, &size,
-                               holder->buf))
+  if (MHD_YES != compress_buf (&holder->stream, src, r_size, &offset, &buf,
+                               &size, holder->buf))
     ret = MHD_CONTENT_READER_END_WITH_ERROR;
   else
   {
     memcpy (mem, buf, size);
-    ret = size;
+    ret = (ssize_t) size;
   }
   free (buf); /* Buf may be set even on error return. */
 done:
diff --git a/src/examples/http_compression.c b/src/examples/http_compression.c
index 0f532cf0..244266ef 100644
--- a/src/examples/http_compression.c
+++ b/src/examples/http_compression.c
@@ -1,6 +1,7 @@
 /*
      This file is part of libmicrohttpd
      Copyright (C) 2019 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2019-2022 Evgeny Grin (Karlson2k)
 
      This library is free software; you can redistribute it and/or
      modify it under the terms of the GNU Lesser General Public
@@ -20,6 +21,7 @@
  * @file http_compression.c
  * @brief minimal example for how to compress HTTP response
  * @author Silvio Clecio (silvioprog)
+ * @author Karlson2k (Evgeny Grin)
  */
 
 #include "platform.h"
@@ -68,14 +70,14 @@ body_compress (void **buf,
   uLongf cbuf_size;
   int ret;
 
-  cbuf_size = compressBound (*buf_size);
+  cbuf_size = compressBound ((uLong) * buf_size);
   cbuf = malloc (cbuf_size);
   if (NULL == cbuf)
     return MHD_NO;
   ret = compress (cbuf,
                   &cbuf_size,
                   (const Bytef *) *buf,
-                  *buf_size);
+                  (uLong) * buf_size);
   if ((Z_OK != ret) ||
       (cbuf_size >= *buf_size))
   {
@@ -165,15 +167,18 @@ int
 main (int argc, char *const *argv)
 {
   struct MHD_Daemon *d;
+  unsigned int port;
 
-  if (argc != 2)
+  if ( (argc != 2) ||
+       (1 != sscanf (argv[1], "%u", &port)) ||
+       (65535 < port) )
   {
     printf ("%s PORT\n", argv[0]);
     return 1;
   }
   d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD
                         | MHD_USE_ERROR_LOG,
-                        atoi (argv[1]), NULL, NULL,
+                        (uint16_t) port, NULL, NULL,
                         &ahc_echo, NULL,
                         MHD_OPTION_END);
   if (NULL == d)
diff --git a/src/examples/https_fileserver_example.c 
b/src/examples/https_fileserver_example.c
index bbfbf2db..30fbf4d8 100644
--- a/src/examples/https_fileserver_example.c
+++ b/src/examples/https_fileserver_example.c
@@ -112,10 +112,19 @@ 
OT1qAbIblaRuWqCsid8BzP7ZQiAnAWgMRSUg1gzDwSwRhrYQRRWAyn/Qipzec+27\n\
 static ssize_t
 file_reader (void *cls, uint64_t pos, char *buf, size_t max)
 {
-  FILE *file = cls;
+  FILE *file = (FILE *) cls;
+  size_t bytes_read;
 
-  (void) fseek (file, pos, SEEK_SET);
-  return fread (buf, 1, max, file);
+  /* 'fseek' may not support files larger 2GiB, depending on platform.
+   * For production code, make sure that 'pos' has valid values, supported by
+   * 'fseek', or use 'fseeko' or similar function. */
+  if (0 != fseek (file, (long) pos, SEEK_SET))
+    return MHD_CONTENT_READER_END_WITH_ERROR;
+  bytes_read = fread (buf, 1, max, file);
+  if (0 == bytes_read)
+    return (0 != ferror (file)) ? MHD_CONTENT_READER_END_WITH_ERROR :
+           MHD_CONTENT_READER_END_OF_STREAM;
+  return (ssize_t) bytes_read;
 }
 
 
@@ -186,7 +195,8 @@ http_ahc (void *cls,
   }
   else
   {
-    response = MHD_create_response_from_callback (buf.st_size, 32 * 1024,      
 /* 32k PAGE_NOT_FOUND size */
+    response = MHD_create_response_from_callback ((size_t) buf.st_size,
+                                                  32 * 1024,   /* 32k page 
size */
                                                   &file_reader, file,
                                                   &file_free_callback);
     if (NULL == response)
diff --git a/src/examples/minimal_example.c b/src/examples/minimal_example.c
index 1e3f63f7..f7a0e64c 100644
--- a/src/examples/minimal_example.c
+++ b/src/examples/minimal_example.c
@@ -82,19 +82,27 @@ main (int argc,
 {
   struct MHD_Daemon *d;
   struct handler_param data_for_handler;
+  int port;
 
   if (argc != 2)
   {
     printf ("%s PORT\n", argv[0]);
     return 1;
   }
+  port = atoi (argv[1]);
+  if ( (1 > port) || (port > 65535) )
+  {
+    fprintf (stderr,
+             "Port must be a number between 1 and 65535.\n");
+    return 1;
+  }
   data_for_handler.response_page = PAGE;
   d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | 
MHD_USE_ERROR_LOG, */
     MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
     /* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */
     /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | 
MHD_USE_ERROR_LOG | MHD_USE_POLL, */
     /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | 
MHD_USE_ERROR_LOG, */
-    atoi (argv[1]),
+    (uint16_t) port,
     NULL, NULL, &ahc_echo, &data_for_handler,
     MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
     MHD_OPTION_STRICT_FOR_CLIENT, (int) 1,
diff --git a/src/examples/minimal_example_comet.c 
b/src/examples/minimal_example_comet.c
index a8864d89..193c6051 100644
--- a/src/examples/minimal_example_comet.c
+++ b/src/examples/minimal_example_comet.c
@@ -1,6 +1,7 @@
 /*
      This file is part of libmicrohttpd
      Copyright (C) 2007, 2008 Christian Grothoff (and other contributing 
authors)
+     Copyright (C) 2016-2022 Evgeny Grin (Karlson2k)
 
      This library is free software; you can redistribute it and/or
      modify it under the terms of the GNU Lesser General Public
@@ -20,6 +21,7 @@
  * @file minimal_example.c
  * @brief minimal example for how to generate an infinite stream with 
libmicrohttpd
  * @author Christian Grothoff
+ * @author Karlson2k (Evgeny Grin)
  */
 
 #include "platform.h"
@@ -77,15 +79,23 @@ int
 main (int argc, char *const *argv)
 {
   struct MHD_Daemon *d;
+  int port;
 
   if (argc != 2)
   {
     printf ("%s PORT\n", argv[0]);
     return 1;
   }
+  port = atoi (argv[1]);
+  if ( (1 > port) || (port > 65535) )
+  {
+    fprintf (stderr,
+             "Port must be a number between 1 and 65535.\n");
+    return 1;
+  }
   d = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_THREAD_PER_CONNECTION
                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
-                        atoi (argv[1]),
+                        (uint16_t) port,
                         NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
   if (d == NULL)
     return 1;
diff --git a/src/examples/minimal_example_empty.c 
b/src/examples/minimal_example_empty.c
index 84837f08..3556d753 100644
--- a/src/examples/minimal_example_empty.c
+++ b/src/examples/minimal_example_empty.c
@@ -1,6 +1,7 @@
 /*
      This file is part of libmicrohttpd
      Copyright (C) 2007 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2022 Evgeny Grin (Karlson2k)
 
      This library is free software; you can redistribute it and/or
      modify it under the terms of the GNU Lesser General Public
@@ -20,6 +21,7 @@
  * @file minimal_example.c
  * @brief minimal example for how to use libmicrohttpd
  * @author Christian Grothoff
+ * @author Karlson2k (Evgeny Grin)
  */
 
 #include "platform.h"
@@ -69,18 +71,26 @@ main (int argc,
       char *const *argv)
 {
   struct MHD_Daemon *d;
+  int port;
 
   if (argc != 2)
   {
     printf ("%s PORT\n", argv[0]);
     return 1;
   }
+  port = atoi (argv[1]);
+  if ( (1 > port) || (port > 65535) )
+  {
+    fprintf (stderr,
+             "Port must be a number between 1 and 65535.\n");
+    return 1;
+  }
   d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | 
MHD_USE_ERROR_LOG, */
     MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
     /* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */
     /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | 
MHD_USE_ERROR_LOG | MHD_USE_POLL, */
     /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | 
MHD_USE_ERROR_LOG, */
-    atoi (argv[1]),
+    (uint16_t) port,
     NULL, NULL, &ahc_echo, NULL,
     MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
     MHD_OPTION_STRICT_FOR_CLIENT, (int) 1,
diff --git a/src/examples/minimal_example_empty_tls.c 
b/src/examples/minimal_example_empty_tls.c
index c8c3763f..e01dc008 100644
--- a/src/examples/minimal_example_empty_tls.c
+++ b/src/examples/minimal_example_empty_tls.c
@@ -1,6 +1,7 @@
 /*
      This file is part of libmicrohttpd
      Copyright (C) 2007 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2021-2022 Evgeny Grin (Karlson2k)
 
      This library is free software; you can redistribute it and/or
      modify it under the terms of the GNU Lesser General Public
@@ -17,9 +18,10 @@
      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 
 USA
 */
 /**
- * @file minimal_example_empty_ssl.c
+ * @file minimal_example_empty_tls.c
  * @brief minimal example for how to use libmicrohttpd
  * @author Christian Grothoff
+ * @author Karlson2k (Evgeny Grin)
  */
 
 #include "platform.h"
@@ -135,19 +137,27 @@ main (int argc,
       char *const *argv)
 {
   struct MHD_Daemon *d;
+  int port;
 
   if (argc != 2)
   {
     printf ("%s PORT\n", argv[0]);
     return 1;
   }
+  port = atoi (argv[1]);
+  if ( (1 > port) || (port > 65535) )
+  {
+    fprintf (stderr,
+             "Port must be a number between 1 and 65535.\n");
+    return 1;
+  }
   d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | 
MHD_USE_ERROR_LOG, */
     MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
     | MHD_USE_TLS,
     /* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */
     /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | 
MHD_USE_ERROR_LOG | MHD_USE_POLL, */
     /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | 
MHD_USE_ERROR_LOG, */
-    atoi (argv[1]),
+    (uint16_t) port,
     NULL, NULL, &ahc_echo, NULL,
     MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
     MHD_OPTION_STRICT_FOR_CLIENT, (int) 1,
diff --git a/src/examples/post_example.c b/src/examples/post_example.c
index 1b6a5a03..6b10afc0 100644
--- a/src/examples/post_example.c
+++ b/src/examples/post_example.c
@@ -736,16 +736,24 @@ main (int argc, char *const *argv)
   fd_set es;
   MHD_socket max;
   uint64_t mhd_timeout;
+  int port;
 
   if (argc != 2)
   {
     printf ("%s PORT\n", argv[0]);
     return 1;
   }
+  port = atoi (argv[1]);
+  if ( (1 > port) || (port > 65535) )
+  {
+    fprintf (stderr,
+             "Port must be a number between 1 and 65535.\n");
+    return 1;
+  }
   /* initialize PRNG */
   srand ((unsigned int) time (NULL));
   d = MHD_start_daemon (MHD_USE_ERROR_LOG,
-                        atoi (argv[1]),
+                        (uint16_t) port,
                         NULL, NULL,
                         &create_response, NULL,
                         MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 15,
@@ -765,13 +773,17 @@ main (int argc, char *const *argv)
       break; /* fatal internal error */
     if (MHD_get_timeout64 (d, &mhd_timeout) == MHD_YES)
     {
-      tv.tv_sec = mhd_timeout / 1000;
-      tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000)) * 1000;
+#if ! defined(_WIN32) || defined(__CYGWIN__)
+      tv.tv_sec = (time_t) (mhd_timeout / 1000LL);
+#else  /* Native W32 */
+      tv.tv_sec = (long) (mhd_timeout / 1000LL);
+#endif /* Native W32 */
+      tv.tv_usec = ((long) (mhd_timeout % 1000)) * 1000;
       tvp = &tv;
     }
     else
       tvp = NULL;
-    if (-1 == select (max + 1, &rs, &ws, &es, tvp))
+    if (-1 == select ((int) max + 1, &rs, &ws, &es, tvp))
     {
       if (EINTR != errno)
         abort ();
diff --git a/src/examples/querystring_example.c 
b/src/examples/querystring_example.c
index 97a92ae2..50b62939 100644
--- a/src/examples/querystring_example.c
+++ b/src/examples/querystring_example.c
@@ -1,6 +1,7 @@
 /*
      This file is part of libmicrohttpd
      Copyright (C) 2007, 2008 Christian Grothoff (and other contributing 
authors)
+     Copyright (C) 2016-2022 Evgeny Grin (Karlson2k)
 
      This library is free software; you can redistribute it and/or
      modify it under the terms of the GNU Lesser General Public
@@ -21,6 +22,7 @@
  * @brief example for how to get the query string from libmicrohttpd
  *        Call with an URI ending with something like "?q=QUERY"
  * @author Christian Grothoff
+ * @author Karlson2k (Evgeny Grin)
  */
 
 #include "platform.h"
@@ -43,6 +45,7 @@ ahc_echo (void *cls,
   struct MHD_Response *response;
   enum MHD_Result ret;
   int resp_len;
+  size_t buf_size;
   (void) cls;               /* Unused. Silent compiler warning. */
   (void) url;               /* Unused. Silent compiler warning. */
   (void) version;           /* Unused. Silent compiler warning. */
@@ -62,18 +65,19 @@ ahc_echo (void *cls,
   if (NULL == val)
     return MHD_NO;  /* No "q" argument was found */
   resp_len = snprintf (NULL, 0, PAGE, "q", val);
-  if (0 > resp_len)
+  if (0 >= resp_len)
     return MHD_NO;  /* Error calculating response size */
-  me = malloc (resp_len + 1);
+  buf_size = (size_t) resp_len + 1; /* Add one byte for zero-termination */
+  me = malloc (buf_size);
   if (me == NULL)
     return MHD_NO;  /* Error allocating memory */
-  if (resp_len != snprintf (me, resp_len + 1, PAGE, "q", val))
+  if (resp_len != snprintf (me, buf_size, PAGE, "q", val))
   {
     free (me);
     return MHD_NO;  /* Error forming the response body */
   }
   response =
-    MHD_create_response_from_buffer_with_free_callback (resp_len,
+    MHD_create_response_from_buffer_with_free_callback (buf_size - 1,
                                                         (void *) me,
                                                         &free);
   if (response == NULL)
diff --git a/src/examples/refuse_post_example.c 
b/src/examples/refuse_post_example.c
index 70cfe4b3..8a8eac37 100644
--- a/src/examples/refuse_post_example.c
+++ b/src/examples/refuse_post_example.c
@@ -33,14 +33,15 @@ struct handler_param
 
 const char *askpage =
   "<html><body>\n\
-                       Upload a file, please!<br>\n\
-                       <form action=\"/filepost\" method=\"post\" 
enctype=\"multipart/form-data\">\n\
-                       <input name=\"file\" type=\"file\">\n\
-                       <input type=\"submit\" value=\" Send \"></form>\n\
-                       </body></html>";
+ Upload a file, please!<br>\n\
+ <form action=\"/filepost\" method=\"post\" enctype=\"multipart/form-data\">\n\
+ <input name=\"file\" type=\"file\">\n\
+ <input type=\"submit\" value=\" Send \"></form>\n\
+ </body></html>";
 
 #define BUSYPAGE \
-  "<html><head><title>Webserver busy</title></head><body>We are too busy to 
process POSTs right now.</body></html>"
+  "<html><head><title>Webserver busy</title></head>" \
+  "<body>We are too busy to process POSTs right now.</body></html>"
 
 static enum MHD_Result
 ahc_echo (void *cls,
@@ -96,18 +97,25 @@ int
 main (int argc, char *const *argv)
 {
   struct MHD_Daemon *d;
-
   struct handler_param data_for_handler;
+  int port;
 
   if (argc != 2)
   {
     printf ("%s PORT\n", argv[0]);
     return 1;
   }
+  port = atoi (argv[1]);
+  if ( (1 > port) || (port > 65535) )
+  {
+    fprintf (stderr,
+             "Port must be a number between 1 and 65535.\n");
+    return 1;
+  }
   data_for_handler.response_page = askpage;
   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
-                        atoi (argv[1]),
+                        (uint16_t) port,
                         NULL, NULL, &ahc_echo, &data_for_handler,
                         MHD_OPTION_END);
   if (d == NULL)
diff --git a/src/examples/suspend_resume_epoll.c 
b/src/examples/suspend_resume_epoll.c
index 87ec6521..c85d9b7c 100644
--- a/src/examples/suspend_resume_epoll.c
+++ b/src/examples/suspend_resume_epoll.c
@@ -145,14 +145,22 @@ main (int argc,
   struct epoll_event events_list[1];
   struct Request *req;
   uint64_t timer_expirations;
+  int port;
 
   if (argc != 2)
   {
     printf ("%s PORT\n", argv[0]);
     return 1;
   }
+  port = atoi (argv[1]);
+  if ( (1 > port) || (port > 65535) )
+  {
+    fprintf (stderr,
+             "Port must be a number between 1 and 65535.\n");
+    return 1;
+  }
   d = MHD_start_daemon (MHD_USE_EPOLL | MHD_ALLOW_SUSPEND_RESUME,
-                        atoi (argv[1]),
+                        (uint16_t) port,
                         NULL, NULL, &ahc_echo, NULL,
                         MHD_OPTION_NOTIFY_COMPLETED, &connection_done, NULL,
                         MHD_OPTION_END);
diff --git a/src/examples/upgrade_example.c b/src/examples/upgrade_example.c
index 547f1b6c..5b57d24e 100644
--- a/src/examples/upgrade_example.c
+++ b/src/examples/upgrade_example.c
@@ -1,6 +1,7 @@
 /*
      This file is part of libmicrohttpd
      Copyright (C) 2016 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2016-2022 Evgeny Grin (Karlson2k)
 
      This library is free software; you can redistribute it and/or
      modify it under the terms of the GNU Lesser General Public
@@ -20,6 +21,7 @@
  * @file upgrade_example.c
  * @brief example for how to use libmicrohttpd upgrade
  * @author Christian Grothoff
+ * @author Karlson2k (Evgeny Grin)
  *
  * Telnet to the HTTP server, use this in the request:
  * GET / http/1.1
@@ -71,11 +73,15 @@ send_all (MHD_socket sock,
   size_t off;
 
   make_blocking (sock);
-  for (off = 0; off < len; off += ret)
+  for (off = 0; off < len; off += (size_t) ret)
   {
     ret = send (sock,
                 &buf[off],
+#if ! defined(_WIN32) || defined(__CYGWIN__)
                 len - off,
+#else  /* Native W32 */
+                (int) (len - off),
+#endif /* Native W32 */
                 0);
     if (0 > ret)
     {
@@ -135,7 +141,7 @@ run_usock (void *cls)
       break;
     send_all (md->sock,
               buf,
-              got);
+              (size_t) got);
   }
   free (md);
   MHD_upgrade_action (urh,
@@ -286,15 +292,18 @@ main (int argc,
       char *const *argv)
 {
   struct MHD_Daemon *d;
+  unsigned int port;
 
-  if (argc != 2)
+  if ( (argc != 2) ||
+       (1 != sscanf (argv[1], "%u", &port)) ||
+       (65535 < port) )
   {
     printf ("%s PORT\n", argv[0]);
     return 1;
   }
   d = MHD_start_daemon (MHD_ALLOW_UPGRADE | MHD_USE_AUTO
                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
-                        atoi (argv[1]),
+                        (uint16_t) port,
                         NULL, NULL,
                         &ahc_echo, NULL,
                         MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h
index dcbc7701..d6445b77 100644
--- a/src/include/microhttpd.h
+++ b/src/include/microhttpd.h
@@ -4969,8 +4969,7 @@ enum MHD_FEATURE
   MHD_FEATURE_HTTPS_CERT_CALLBACK2 = 23,
 
   /**
-   * Get whether option automatic parsing of HTTP Cookie header
-   * is enabled.
+   * Get whether automatic parsing of HTTP Cookie header is supported.
    * If disabled, no MHD_COOKIE_KIND will be generated by MHD.
    * MHD versions before 0x00097514 always support cookie parsing.
    * @note Available since #MHD_VERSION 0x00097514
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c
index d74535cf..8e780811 100644
--- a/src/microhttpd/connection.c
+++ b/src/microhttpd/connection.c
@@ -5329,14 +5329,22 @@ MHD_get_connection_info (struct MHD_Connection 
*connection,
   case MHD_CONNECTION_INFO_CIPHER_ALGO:
     if (NULL == connection->tls_session)
       return NULL;
-    connection->connection_info_dummy.cipher_algorithm =
-      (int) gnutls_cipher_get (connection->tls_session);
+    if (1)
+    { /* Workaround to mute compiler warning */
+      gnutls_cipher_algorithm_t res;
+      res = gnutls_cipher_get (connection->tls_session);
+      connection->connection_info_dummy.cipher_algorithm = (int) res;
+    }
     return &connection->connection_info_dummy;
   case MHD_CONNECTION_INFO_PROTOCOL:
     if (NULL == connection->tls_session)
       return NULL;
-    connection->connection_info_dummy.protocol =
-      (int) gnutls_protocol_get_version (connection->tls_session);
+    if (1)
+    { /* Workaround to mute compiler warning */
+      gnutls_protocol_t res;
+      res = gnutls_protocol_get_version (connection->tls_session);
+      connection->connection_info_dummy.protocol = (int) res;
+    }
     return &connection->connection_info_dummy;
   case MHD_CONNECTION_INFO_GNUTLS_SESSION:
     if (NULL == connection->tls_session)

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

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