From 9c41e4eb2f336458ac85e356f27db7d0f89a43c9 Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Tue, 30 Jan 2024 12:16:26 +0000 Subject: [PATCH] socket-util: check for sysconf() error before using value Otherwise -1 will be casted to uint32_t. Found by coverity. CID#1533989 Follow-up for 7e8aa5c2eebd86efe9bbf36d8db1e98964611aab --- src/basic/socket-util.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index a9ad1cc9936..68e6afc67fc 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -930,13 +930,15 @@ int getpeersec(int fd, char **ret) { } int getpeergroups(int fd, gid_t **ret) { + socklen_t n = sizeof(gid_t) * 64U; _cleanup_free_ gid_t *d = NULL; assert(fd >= 0); assert(ret); long ngroups_max = sysconf(_SC_NGROUPS_MAX); - socklen_t n = sizeof(gid_t) * MAX((socklen_t) ngroups_max, 64U); + if (ngroups_max > 0) + n = MAX(n, sizeof(gid_t) * (socklen_t) ngroups_max); for (;;) { d = malloc(n); -- 2.47.3