aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/mhd_compat.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/mhd_compat.c')
-rw-r--r--src/microhttpd/mhd_compat.c34
1 files changed, 34 insertions, 0 deletions
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
96void *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 */