diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-11-07 19:35:02 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2016-11-07 21:03:07 +0300 |
commit | 24725d33d9074b880b7f993165dd408153004862 (patch) | |
tree | 1f62fb3e15ba7f8a7f3ad4f202f71921a4feec0d | |
parent | 45dabc9d2503645cbf7a4154ff1f428adfb2b8f4 (diff) | |
download | libmicrohttpd-24725d33d9074b880b7f993165dd408153004862.tar.gz libmicrohttpd-24725d33d9074b880b7f993165dd408153004862.zip |
Used calloc() where possible with fallback to malloc()+memset().
-rw-r--r-- | configure.ac | 14 | ||||
-rw-r--r-- | src/microhttpd/daemon.c | 15 | ||||
-rw-r--r-- | src/microhttpd/mhd_compat.c | 34 | ||||
-rw-r--r-- | src/microhttpd/mhd_compat.h | 12 | ||||
-rw-r--r-- | src/microhttpd/postprocessor.c | 6 | ||||
-rw-r--r-- | src/microhttpd/response.c | 19 |
6 files changed, 70 insertions, 30 deletions
diff --git a/configure.ac b/configure.ac index 7a74e240..60ad6c11 100644 --- a/configure.ac +++ b/configure.ac | |||
@@ -1173,6 +1173,20 @@ AS_VAR_IF([[enable_httpupgrade]],[["yes"]], | |||
1173 | AM_CONDITIONAL([ENABLE_UPGRADE], [[test "x$enable_httpupgrade" = "xyes"]]) | 1173 | AM_CONDITIONAL([ENABLE_UPGRADE], [[test "x$enable_httpupgrade" = "xyes"]]) |
1174 | AC_MSG_RESULT([[$enable_httpupgrade]]) | 1174 | AC_MSG_RESULT([[$enable_httpupgrade]]) |
1175 | 1175 | ||
1176 | AC_CACHE_CHECK([[for calloc()]], [[mhd_cv_have_func_calloc]], | ||
1177 | [ | ||
1178 | AC_LINK_IFELSE([AC_LANG_PROGRAM([[ | ||
1179 | #include <stdlib.h> | ||
1180 | ]],[[void * ptr = calloc(1, 2)]]) | ||
1181 | ], | ||
1182 | [[mhd_cv_have_func_calloc='yes']], | ||
1183 | [[mhd_cv_have_func_calloc='no']] | ||
1184 | ) | ||
1185 | ] | ||
1186 | ) | ||
1187 | AS_VAR_IF([[mhd_cv_have_func_calloc]], [["yes"]], | ||
1188 | [AC_DEFINE([[HAVE_CALLOC]], [[1]], [Define to 1 if you have the usable `calloc' function.])]) | ||
1189 | |||
1176 | # Check for fork() and waitpid(). They are used for tests. | 1190 | # Check for fork() and waitpid(). They are used for tests. |
1177 | AC_MSG_CHECKING([[for fork()]]) | 1191 | AC_MSG_CHECKING([[for fork()]]) |
1178 | AC_LINK_IFELSE( | 1192 | AC_LINK_IFELSE( |
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c index 02a2f143..d7691754 100644 --- a/src/microhttpd/daemon.c +++ b/src/microhttpd/daemon.c | |||
@@ -2062,7 +2062,7 @@ internal_add_connection (struct MHD_Daemon *daemon, | |||
2062 | return MHD_NO; | 2062 | return MHD_NO; |
2063 | } | 2063 | } |
2064 | 2064 | ||
2065 | if (NULL == (connection = malloc (sizeof (struct MHD_Connection)))) | 2065 | if (NULL == (connection = MHD_calloc_ (1, sizeof (struct MHD_Connection)))) |
2066 | { | 2066 | { |
2067 | eno = errno; | 2067 | eno = errno; |
2068 | #ifdef HAVE_MESSAGES | 2068 | #ifdef HAVE_MESSAGES |
@@ -2077,9 +2077,6 @@ internal_add_connection (struct MHD_Daemon *daemon, | |||
2077 | errno = eno; | 2077 | errno = eno; |
2078 | return MHD_NO; | 2078 | return MHD_NO; |
2079 | } | 2079 | } |
2080 | memset (connection, | ||
2081 | 0, | ||
2082 | sizeof (struct MHD_Connection)); | ||
2083 | connection->pool = MHD_pool_create (daemon->pool_size); | 2080 | connection->pool = MHD_pool_create (daemon->pool_size); |
2084 | if (NULL == connection->pool) | 2081 | if (NULL == connection->pool) |
2085 | { | 2082 | { |
@@ -3248,7 +3245,7 @@ MHD_poll_all (struct MHD_Daemon *daemon, | |||
3248 | int poll_itc_idx; | 3245 | int poll_itc_idx; |
3249 | struct pollfd *p; | 3246 | struct pollfd *p; |
3250 | 3247 | ||
3251 | p = malloc (sizeof (struct pollfd) * (2 + num_connections)); | 3248 | p = MHD_calloc_ ((2 + num_connections), sizeof (struct pollfd)); |
3252 | if (NULL == p) | 3249 | if (NULL == p) |
3253 | { | 3250 | { |
3254 | #ifdef HAVE_MESSAGES | 3251 | #ifdef HAVE_MESSAGES |
@@ -3258,9 +3255,6 @@ MHD_poll_all (struct MHD_Daemon *daemon, | |||
3258 | #endif | 3255 | #endif |
3259 | return MHD_NO; | 3256 | return MHD_NO; |
3260 | } | 3257 | } |
3261 | memset (p, | ||
3262 | 0, | ||
3263 | sizeof (struct pollfd) * (2 + num_connections)); | ||
3264 | poll_server = 0; | 3258 | poll_server = 0; |
3265 | poll_listen = -1; | 3259 | poll_listen = -1; |
3266 | if ( (MHD_INVALID_SOCKET != daemon->socket_fd) && | 3260 | if ( (MHD_INVALID_SOCKET != daemon->socket_fd) && |
@@ -4746,11 +4740,8 @@ MHD_start_daemon_va (unsigned int flags, | |||
4746 | } | 4740 | } |
4747 | if (NULL == dh) | 4741 | if (NULL == dh) |
4748 | return NULL; | 4742 | return NULL; |
4749 | if (NULL == (daemon = malloc (sizeof (struct MHD_Daemon)))) | 4743 | if (NULL == (daemon = MHD_calloc_ (1, sizeof (struct MHD_Daemon)))) |
4750 | return NULL; | 4744 | return NULL; |
4751 | memset (daemon, | ||
4752 | 0, | ||
4753 | sizeof (struct MHD_Daemon)); | ||
4754 | #ifdef EPOLL_SUPPORT | 4745 | #ifdef EPOLL_SUPPORT |
4755 | daemon->epoll_fd = -1; | 4746 | daemon->epoll_fd = -1; |
4756 | #if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT) | 4747 | #if defined(HTTPS_SUPPORT) && defined(UPGRADE_SUPPORT) |
diff --git a/src/microhttpd/mhd_compat.c b/src/microhttpd/mhd_compat.c index 976ed03e..3abdc367 100644 --- a/src/microhttpd/mhd_compat.c +++ b/src/microhttpd/mhd_compat.c | |||
@@ -34,6 +34,9 @@ | |||
34 | #endif /* HAVE_SNPRINTF */ | 34 | #endif /* HAVE_SNPRINTF */ |
35 | #endif /* _WIN32 && !__CYGWIN__ */ | 35 | #endif /* _WIN32 && !__CYGWIN__ */ |
36 | 36 | ||
37 | #ifndef HAVE_CALLOC | ||
38 | #include <string.h> /* for memset() */ | ||
39 | #endif /* ! HAVE_CALLOC */ | ||
37 | 40 | ||
38 | #if defined(_WIN32) && !defined(__CYGWIN__) | 41 | #if defined(_WIN32) && !defined(__CYGWIN__) |
39 | 42 | ||
@@ -78,3 +81,34 @@ W32_snprintf (char *__restrict s, | |||
78 | 81 | ||
79 | #endif /* HAVE_SNPRINTF */ | 82 | #endif /* HAVE_SNPRINTF */ |
80 | #endif /* _WIN32 && !__CYGWIN__ */ | 83 | #endif /* _WIN32 && !__CYGWIN__ */ |
84 | |||
85 | #ifndef HAVE_CALLOC | ||
86 | |||
87 | #ifdef __has_builtin | ||
88 | # if __has_builtin(__builtin_mul_overflow) | ||
89 | # define MHD_HAVE_NUL_OVERFLOW 1 | ||
90 | # endif | ||
91 | #elif __GNUC__+0 >= 5 | ||
92 | # define MHD_HAVE_NUL_OVERFLOW 1 | ||
93 | #endif /* __GNUC__ >= 5 */ | ||
94 | |||
95 | |||
96 | void *MHD_calloc_(size_t nelem, size_t elsize) | ||
97 | { | ||
98 | size_t alloc_size; | ||
99 | void *ptr; | ||
100 | #ifdef MHD_HAVE_NUL_OVERFLOW | ||
101 | if (__builtin_mul_overflow(nelem, elsize, &alloc_size) || 0 == alloc_size) | ||
102 | return NULL; | ||
103 | #else /* ! MHD_HAVE_NUL_OVERFLOW */ | ||
104 | alloc_size = nelem * elsize; | ||
105 | if (0 == alloc_size || elsize != alloc_size / nelem) | ||
106 | return NULL; | ||
107 | #endif /* ! MHD_HAVE_NUL_OVERFLOW */ | ||
108 | ptr = malloc (alloc_size); | ||
109 | if (NULL == ptr) | ||
110 | return NULL; | ||
111 | memset(ptr, 0, alloc_size); | ||
112 | return ptr; | ||
113 | } | ||
114 | #endif /* ! HAVE_CALLOC */ | ||
diff --git a/src/microhttpd/mhd_compat.h b/src/microhttpd/mhd_compat.h index 19ebac5f..fca84b84 100644 --- a/src/microhttpd/mhd_compat.h +++ b/src/microhttpd/mhd_compat.h | |||
@@ -72,4 +72,16 @@ int W32_snprintf(char *__restrict s, size_t n, const char *__restrict format, .. | |||
72 | #endif /* HAVE_RAND */ | 72 | #endif /* HAVE_RAND */ |
73 | #endif /* HAVE_RANDOM */ | 73 | #endif /* HAVE_RANDOM */ |
74 | 74 | ||
75 | #ifdef HAVE_CALLOC | ||
76 | /** | ||
77 | * MHD_calloc_ is platform-independent calloc() | ||
78 | */ | ||
79 | #define MHD_calloc_(n,s) calloc((n),(s)) | ||
80 | #else /* ! HAVE_CALLOC */ | ||
81 | /** | ||
82 | * MHD_calloc_ is platform-independent calloc() | ||
83 | */ | ||
84 | void *MHD_calloc_(size_t nelem, size_t elsize); | ||
85 | #endif /* ! HAVE_CALLOC */ | ||
86 | |||
75 | #endif /* MHD_COMPAT_H */ | 87 | #endif /* MHD_COMPAT_H */ |
diff --git a/src/microhttpd/postprocessor.c b/src/microhttpd/postprocessor.c index 287e03a2..cf3c31ec 100644 --- a/src/microhttpd/postprocessor.c +++ b/src/microhttpd/postprocessor.c | |||
@@ -25,6 +25,7 @@ | |||
25 | 25 | ||
26 | #include "internal.h" | 26 | #include "internal.h" |
27 | #include "mhd_str.h" | 27 | #include "mhd_str.h" |
28 | #include "mhd_compat.h" | ||
28 | 29 | ||
29 | /** | 30 | /** |
30 | * Size of on-stack buffer that we use for un-escaping of the value. | 31 | * Size of on-stack buffer that we use for un-escaping of the value. |
@@ -325,11 +326,8 @@ MHD_create_post_processor (struct MHD_Connection *connection, | |||
325 | buffer_size += 4; /* round up to get nice block sizes despite boundary search */ | 326 | buffer_size += 4; /* round up to get nice block sizes despite boundary search */ |
326 | 327 | ||
327 | /* add +1 to ensure we ALWAYS have a zero-termination at the end */ | 328 | /* add +1 to ensure we ALWAYS have a zero-termination at the end */ |
328 | if (NULL == (ret = malloc (sizeof (struct MHD_PostProcessor) + buffer_size + 1))) | 329 | if (NULL == (ret = MHD_calloc_ (1, sizeof (struct MHD_PostProcessor) + buffer_size + 1))) |
329 | return NULL; | 330 | return NULL; |
330 | memset (ret, | ||
331 | 0, | ||
332 | sizeof (struct MHD_PostProcessor) + buffer_size + 1); | ||
333 | ret->connection = connection; | 331 | ret->connection = connection; |
334 | ret->ikvi = iter; | 332 | ret->ikvi = iter; |
335 | ret->cls = iter_cls; | 333 | ret->cls = iter_cls; |
diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c index 17fe8122..8b89c5f0 100644 --- a/src/microhttpd/response.c +++ b/src/microhttpd/response.c | |||
@@ -38,6 +38,7 @@ | |||
38 | #include "mhd_itc.h" | 38 | #include "mhd_itc.h" |
39 | #include "connection.h" | 39 | #include "connection.h" |
40 | #include "memorypool.h" | 40 | #include "memorypool.h" |
41 | #include "mhd_compat.h" | ||
41 | 42 | ||
42 | 43 | ||
43 | #if defined(_WIN32) && defined(MHD_W32_MUTEX_) | 44 | #if defined(_WIN32) && defined(MHD_W32_MUTEX_) |
@@ -270,11 +271,8 @@ MHD_create_response_from_callback (uint64_t size, | |||
270 | 271 | ||
271 | if ((NULL == crc) || (0 == block_size)) | 272 | if ((NULL == crc) || (0 == block_size)) |
272 | return NULL; | 273 | return NULL; |
273 | if (NULL == (response = malloc (sizeof (struct MHD_Response) + block_size))) | 274 | if (NULL == (response = MHD_calloc_ (1, sizeof (struct MHD_Response) + block_size))) |
274 | return NULL; | 275 | return NULL; |
275 | memset (response, | ||
276 | 0, | ||
277 | sizeof (struct MHD_Response)); | ||
278 | response->fd = -1; | 276 | response->fd = -1; |
279 | response->data = (void *) &response[1]; | 277 | response->data = (void *) &response[1]; |
280 | response->data_buffer_size = block_size; | 278 | response->data_buffer_size = block_size; |
@@ -554,11 +552,8 @@ MHD_create_response_from_data (size_t size, | |||
554 | 552 | ||
555 | if ((NULL == data) && (size > 0)) | 553 | if ((NULL == data) && (size > 0)) |
556 | return NULL; | 554 | return NULL; |
557 | if (NULL == (response = malloc (sizeof (struct MHD_Response)))) | 555 | if (NULL == (response = MHD_calloc_ (1, sizeof (struct MHD_Response)))) |
558 | return NULL; | 556 | return NULL; |
559 | memset (response, | ||
560 | 0, | ||
561 | sizeof (struct MHD_Response)); | ||
562 | response->fd = -1; | 557 | response->fd = -1; |
563 | if (! MHD_mutex_init_ (&response->mutex)) | 558 | if (! MHD_mutex_init_ (&response->mutex)) |
564 | { | 559 | { |
@@ -706,12 +701,9 @@ MHD_response_execute_upgrade_ (struct MHD_Response *response, | |||
706 | return MHD_NO; | 701 | return MHD_NO; |
707 | } | 702 | } |
708 | 703 | ||
709 | urh = malloc (sizeof (struct MHD_UpgradeResponseHandle)); | 704 | urh = MHD_calloc_ (1, sizeof (struct MHD_UpgradeResponseHandle)); |
710 | if (NULL == urh) | 705 | if (NULL == urh) |
711 | return MHD_NO; | 706 | return MHD_NO; |
712 | memset (urh, | ||
713 | 0, | ||
714 | sizeof (struct MHD_UpgradeResponseHandle)); | ||
715 | urh->connection = connection; | 707 | urh->connection = connection; |
716 | rbo = connection->read_buffer_offset; | 708 | rbo = connection->read_buffer_offset; |
717 | connection->read_buffer_offset = 0; | 709 | connection->read_buffer_offset = 0; |
@@ -960,10 +952,9 @@ MHD_create_response_for_upgrade (MHD_UpgradeHandler upgrade_handler, | |||
960 | 952 | ||
961 | if (NULL == upgrade_handler) | 953 | if (NULL == upgrade_handler) |
962 | return NULL; /* invalid request */ | 954 | return NULL; /* invalid request */ |
963 | response = malloc (sizeof (struct MHD_Response)); | 955 | response = MHD_calloc_ (1, sizeof (struct MHD_Response)); |
964 | if (NULL == response) | 956 | if (NULL == response) |
965 | return NULL; | 957 | return NULL; |
966 | memset (response, 0, sizeof (struct MHD_Response)); | ||
967 | if (! MHD_mutex_init_ (&response->mutex)) | 958 | if (! MHD_mutex_init_ (&response->mutex)) |
968 | { | 959 | { |
969 | free (response); | 960 | free (response); |