commit b6eb14d6469a5d48df977cfcd3b38f2d19e5951f
parent 57cc8b71135a2d79240bf7edc678697a7192d208
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date: Mon, 18 Apr 2022 13:11:26 +0300
Added workaround for external APIs
Some APIs require non-const pointer even when data is supposed to be
unmodifiable. Added workaround to deal with such APIs without compiler
warnings.
Diffstat:
4 files changed, 17 insertions(+), 10 deletions(-)
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
@@ -534,7 +534,7 @@ MHD_init_daemon_certificate (struct MHD_Daemon *daemon)
#endif
return -1;
}
- cert.data = (unsigned char *) daemon->https_mem_trust;
+ cert.data = (unsigned char *) _MHD_DROP_CONST (daemon->https_mem_trust);
cert.size = (unsigned int) paramlen;
if (gnutls_certificate_set_x509_trust_mem (daemon->x509_cred,
&cert,
@@ -571,9 +571,9 @@ MHD_init_daemon_certificate (struct MHD_Daemon *daemon)
#endif
return -1;
}
- key.data = (unsigned char *) daemon->https_mem_key;
+ key.data = (unsigned char *) _MHD_DROP_CONST (daemon->https_mem_key);
key.size = (unsigned int) param1len;
- cert.data = (unsigned char *) daemon->https_mem_cert;
+ cert.data = (unsigned char *) _MHD_DROP_CONST (daemon->https_mem_cert);
cert.size = (unsigned int) param2len;
if (NULL != daemon->https_key_password)
@@ -4948,7 +4948,7 @@ MHD_epoll (struct MHD_Daemon *daemon,
(-1 != daemon->epoll_upgrade_fd) ) )
{
event.events = EPOLLIN | EPOLLOUT;
- event.data.ptr = (void *) upgrade_marker;
+ event.data.ptr = _MHD_DROP_CONST (upgrade_marker);
if (0 != epoll_ctl (daemon->epoll_fd,
EPOLL_CTL_ADD,
daemon->epoll_upgrade_fd,
@@ -5850,7 +5850,7 @@ parse_options_va (struct MHD_Daemon *daemon,
#endif
return MHD_NO;
}
- dhpar.data = (unsigned char *) pstr;
+ dhpar.data = (unsigned char *) _MHD_DROP_CONST (pstr);
pstr_len = strlen (pstr);
if (UINT_MAX < pstr_len)
{
@@ -6340,7 +6340,7 @@ setup_epoll_to_listen (struct MHD_Daemon *daemon)
if (MHD_ITC_IS_VALID_ (daemon->itc))
{
event.events = EPOLLIN;
- event.data.ptr = (void *) epoll_itc_marker;
+ event.data.ptr = _MHD_DROP_CONST (epoll_itc_marker);
if (0 != epoll_ctl (daemon->epoll_fd,
EPOLL_CTL_ADD,
MHD_itc_r_fd_ (daemon->itc),
diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h
@@ -63,6 +63,13 @@
#include "mhd_sockets.h"
#include "mhd_itc_types.h"
+/**
+ * Macro to drop 'const' qualifier from pointer without compiler warning.
+ * To be used *only* to deal with broken external APIs, which require non-const
+ * pointer to unmodifiable data.
+ * Must not be used to transform pointers for MHD needs.
+ */
+#define _MHD_DROP_CONST(ptr) ((void *)((uintptr_t)((const void *)(ptr))))
/**
* @def _MHD_MACRO_NO
diff --git a/src/microhttpd/mhd_send.c b/src/microhttpd/mhd_send.c
@@ -1054,9 +1054,9 @@ MHD_send_hdr_and_body_ (struct MHD_Connection *connection,
#endif /* ! HAVE_SENDMSG */
push_hdr || push_body);
#if defined(HAVE_SENDMSG) || defined(HAVE_WRITEV)
- vector[0].iov_base = (void *) header;
+ vector[0].iov_base = _MHD_DROP_CONST (header);
vector[0].iov_len = header_size;
- vector[1].iov_base = (void *) body;
+ vector[1].iov_base = _MHD_DROP_CONST (body);
vector[1].iov_len = body_size;
#if defined(HAVE_SENDMSG)
diff --git a/src/microhttpd/response.c b/src/microhttpd/response.c
@@ -1623,14 +1623,14 @@ MHD_create_response_from_iovec (const struct MHD_IoVec *iov,
#if defined(MHD_WINSOCK_SOCKETS) && defined(_WIN64)
while (MHD_IOV_ELMN_MAX_SIZE < element_size)
{
- iov_copy[i_cp].iov_base = (char *) buf;
+ iov_copy[i_cp].iov_base = (char *) _MHD_DROP_CONST (buf);
iov_copy[i_cp].iov_len = ULONG_MAX;
buf += ULONG_MAX;
element_size -= ULONG_MAX;
i_cp++;
}
#endif /* MHD_WINSOCK_SOCKETS && _WIN64 */
- iov_copy[i_cp].iov_base = (void *) buf;
+ iov_copy[i_cp].iov_base = _MHD_DROP_CONST (buf);
iov_copy[i_cp].iov_len = (MHD_iov_size_) element_size;
i_cp++;
}