mirror of
https://passt.top/passt
synced 2025-05-22 01:05:35 +02:00
util: Consolidate and improve workarounds for clang-tidy issue 58992
We have several workarounds for a clang-tidy bug where the checker doesn't recognize that a number of system calls write to - and therefore initialise - a socket address. We can't neatly use a suppression, because the bogus warning shows up some time after the actual system call, when we access a field of the socket address which clang-tidy erroneously thinks is uninitialised. Consolidate these workarounds into one place by using macros to implement wrappers around affected system calls which add a memset() of the sockaddr to silence clang-tidy. This removes the need for the individual memset() workarounds at the callers - and the somewhat longwinded explanatory comments. We can then use a #define to not include the hack in "real" builds, but only consider it for clang-tidy. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
parent
5b6c68c2e4
commit
c1d2a070f2
4 changed files with 43 additions and 13 deletions
41
util.h
41
util.h
|
@ -9,6 +9,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
|
@ -225,4 +226,44 @@ int __daemon(int pidfile_fd, int devnull_fd);
|
|||
int fls(unsigned long x);
|
||||
int write_file(const char *path, const char *buf);
|
||||
|
||||
/*
|
||||
* Workarounds for https://github.com/llvm/llvm-project/issues/58992
|
||||
*
|
||||
* For a number (maybe all) system calls that _write_ a socket address,
|
||||
* clang-tidy doesn't register that the memory of the socket address will be
|
||||
* initialised after the call. This can't easily be worked around with
|
||||
* clang-tidy suppressions, because the warning doesn't show on the syscall
|
||||
* itself but later when we access the supposedly uninitialised field.
|
||||
*/
|
||||
static inline void sa_init(struct sockaddr *sa, socklen_t *sl)
|
||||
{
|
||||
#ifdef CLANG_TIDY_58992
|
||||
if (sa)
|
||||
memset(sa, 0, *sl);
|
||||
#else
|
||||
(void)sa;
|
||||
(void)sl;
|
||||
#endif /* CLANG_TIDY_58992 */
|
||||
}
|
||||
|
||||
static inline ssize_t wrap_recvfrom(int sockfd, void *buf, size_t len,
|
||||
int flags,
|
||||
struct sockaddr *src_addr,
|
||||
socklen_t *addrlen)
|
||||
{
|
||||
sa_init(src_addr, addrlen);
|
||||
return recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
|
||||
}
|
||||
#define recvfrom(s, buf, len, flags, src, addrlen) \
|
||||
wrap_recvfrom((s), (buf), (len), (flags), (src), (addrlen))
|
||||
|
||||
static inline int wrap_accept4(int sockfd, struct sockaddr *addr,
|
||||
socklen_t *addrlen, int flags)
|
||||
{
|
||||
sa_init(addr, addrlen);
|
||||
return accept4(sockfd, addr, addrlen, flags);
|
||||
}
|
||||
#define accept4(s, addr, addrlen, flags) \
|
||||
wrap_accept4((s), (addr), (addrlen), (flags))
|
||||
|
||||
#endif /* UTIL_H */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue