From 1a23dd25c36bdb0e0be264730e9088a49a1e8152 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 5 Oct 2017 23:16:19 +0200 Subject: misc style improvements, fixing some tiny rare memory leaks in examples --- src/examples/chunked_example.c | 35 +++++++++++++++++++++------ src/examples/demo.c | 6 ++--- src/examples/https_fileserver_example.c | 42 +++++++++++++++++++-------------- src/examples/post_example.c | 22 ++++++++++++----- 4 files changed, 71 insertions(+), 34 deletions(-) (limited to 'src/examples') diff --git a/src/examples/chunked_example.c b/src/examples/chunked_example.c index a661216c..9bf73e34 100644 --- a/src/examples/chunked_example.c +++ b/src/examples/chunked_example.c @@ -32,6 +32,7 @@ struct ResponseContentCallbackParam size_t response_size; }; + static ssize_t callback (void *cls, uint64_t pos, @@ -77,12 +78,14 @@ callback (void *cls, return size_to_copy; } -void -free_callback_param(void *cls) + +static void +free_callback_param (void *cls) { free(cls); } + static const char simple_response_text[] = "Simple response" "Simple response text"; @@ -93,10 +96,12 @@ ahc_echo (void *cls, const char *url, const char *method, const char *version, - const char *upload_data, size_t *upload_data_size, void **ptr) + const char *upload_data, + size_t *upload_data_size, + void **ptr) { static int aptr; - struct ResponseContentCallbackParam * callback_param; + struct ResponseContentCallbackParam *callback_param; struct MHD_Response *response; int ret; (void)cls; /* Unused. Silent compiler warning. */ @@ -127,31 +132,47 @@ ahc_echo (void *cls, &callback, callback_param, &free_callback_param); + if (NULL == response) + { + free (callback_param); + return MHD_NO; + } ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_destroy_response (response); return ret; } + 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 > UINT16_MAX) ) + { + 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]), - NULL, NULL, &ahc_echo, NULL, + (uint16_t) port, + NULL, NULL, + &ahc_echo, NULL, MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, MHD_OPTION_END); - if (d == NULL) + if (NULL == d) return 1; (void) getc (stdin); MHD_stop_daemon (d); diff --git a/src/examples/demo.c b/src/examples/demo.c index edf38e98..0b177357 100644 --- a/src/examples/demo.c +++ b/src/examples/demo.c @@ -254,7 +254,7 @@ struct ResponseDataContext * * @param rdc where to store the list of files * @param dirname name of the directory to list - * @return MHD_YES on success, MHD_NO on error + * @return #MHD_YES on success, #MHD_NO on error */ static int list_directory (struct ResponseDataContext *rdc, @@ -271,7 +271,7 @@ list_directory (struct ResponseDataContext *rdc, { if ('.' == de->d_name[0]) continue; - if (sizeof (fullname) <= (size_t) + if (sizeof (fullname) <= (unsigned int) snprintf (fullname, sizeof (fullname), "%s/%s", dirname, de->d_name)) @@ -555,7 +555,7 @@ process_upload_data (void *cls, uc->category, filename); for (i=strlen (fn)-1;i>=0;i--) - if (! isprint ((int) fn[i])) + if (! isprint ((unsigned char) fn[i])) fn[i] = '_'; uc->fd = open (fn, O_CREAT | O_EXCL diff --git a/src/examples/https_fileserver_example.c b/src/examples/https_fileserver_example.c index 453ca2ff..818c8d55 100644 --- a/src/examples/https_fileserver_example.c +++ b/src/examples/https_fileserver_example.c @@ -191,38 +191,44 @@ int main (int argc, char *const *argv) { struct MHD_Daemon *TLS_daemon; + int port; - if (argc == 2) + if (argc != 2) { - /* TODO check if this is truly necessary - disallow usage of the blocking /dev/random */ - /* gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0); */ - TLS_daemon = - MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | - MHD_USE_TLS, atoi (argv[1]), NULL, NULL, &http_ahc, - NULL, MHD_OPTION_CONNECTION_TIMEOUT, 256, - MHD_OPTION_HTTPS_MEM_KEY, key_pem, - MHD_OPTION_HTTPS_MEM_CERT, cert_pem, - MHD_OPTION_END); + printf ("%s PORT\n", argv[0]); + return 1; } - else + port = atoi (argv[1]); + if ( (1 > port) || + (port > UINT16_MAX) ) { - printf ("Usage: %s HTTP-PORT\n", argv[0]); + fprintf (stderr, + "Port must be a number between 1 and 65535\n"); return 1; } - if (TLS_daemon == NULL) + /* TODO check if this is truly necessary - disallow usage of the blocking /dev/random */ + /* gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0); */ + TLS_daemon = + MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | + MHD_USE_TLS, + (uint16_t) port, + NULL, NULL, + &http_ahc, NULL, + MHD_OPTION_CONNECTION_TIMEOUT, 256, + MHD_OPTION_HTTPS_MEM_KEY, key_pem, + MHD_OPTION_HTTPS_MEM_CERT, cert_pem, + MHD_OPTION_END); + if (NULL == TLS_daemon) { fprintf (stderr, "Error: failed to start TLS_daemon\n"); return 1; } - else - { - printf ("MHD daemon listening on port %d\n", atoi (argv[1])); - } + printf ("MHD daemon listening on port %u\n", + (unsigned int) port); (void) getc (stdin); MHD_stop_daemon (TLS_daemon); - return 0; } diff --git a/src/examples/post_example.c b/src/examples/post_example.c index 8b92956d..6c9a3f4b 100644 --- a/src/examples/post_example.c +++ b/src/examples/post_example.c @@ -313,23 +313,28 @@ fill_v1_form (const void *cls, struct MHD_Connection *connection) { int ret; + size_t slen; char *reply; struct MHD_Response *response; (void)cls; /* Unused. Silent compiler warning. */ - reply = malloc (strlen (MAIN_PAGE) + strlen (session->value_1) + 1); + slen = strlen (MAIN_PAGE) + strlen (session->value_1); + reply = malloc (slen + 1); if (NULL == reply) return MHD_NO; snprintf (reply, - strlen (MAIN_PAGE) + strlen (session->value_1) + 1, + slen + 1, MAIN_PAGE, session->value_1); /* return static form */ - response = MHD_create_response_from_buffer (strlen (reply), + response = MHD_create_response_from_buffer (slen, (void *) reply, MHD_RESPMEM_MUST_FREE); if (NULL == response) + { + free (reply); return MHD_NO; + } add_session_cookie (session, response); MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_ENCODING, @@ -359,22 +364,27 @@ fill_v1_v2_form (const void *cls, int ret; char *reply; struct MHD_Response *response; + size_t slen; (void)cls; /* Unused. Silent compiler warning. */ - reply = malloc (strlen (SECOND_PAGE) + strlen (session->value_1) + strlen (session->value_2) + 1); + slen = strlen (SECOND_PAGE) + strlen (session->value_1) + strlen (session->value_2); + reply = malloc (slen + 1); if (NULL == reply) return MHD_NO; snprintf (reply, - strlen (SECOND_PAGE) + strlen (session->value_1) + strlen (session->value_2) + 1, + slen + 1, SECOND_PAGE, session->value_1, session->value_2); /* return static form */ - response = MHD_create_response_from_buffer (strlen (reply), + response = MHD_create_response_from_buffer (slen, (void *) reply, MHD_RESPMEM_MUST_FREE); if (NULL == response) + { + free (reply); return MHD_NO; + } add_session_cookie (session, response); MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_ENCODING, -- cgit v1.2.3