git: fda64d005e4e - stable/15 - initgroups(3): Fix return value on allocation failure

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Tue, 23 Sep 2025 12:03:46 UTC
The branch stable/15 has been updated by olce:

URL: https://cgit.FreeBSD.org/src/commit/?id=fda64d005e4e9d06166d38fc5e0a26c7d03e61be

commit fda64d005e4e9d06166d38fc5e0a26c7d03e61be
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2025-09-16 15:52:20 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2025-09-23 12:02:46 +0000

    initgroups(3): Fix return value on allocation failure
    
    We must not return ENOMEM, but rather -1 with 'errno' set to ENOMEM, as
    described in the manual page and as other implementations are doing.
    A malloc() failure actually already sets ENOMEM for us.  Add comments
    indicating which function set 'errno' each time we return.
    
    While here, improve style and remove useless headers.
    
    Reviewed by:    kib, emaste
    Fixes:          54404cfb13d4 ("In preparation for raising NGROUPS and NGROUPS_MAX, ...")
    MFC after:      5 days
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D52580
    
    (cherry picked from commit 0b018cfd81d8fdd64af3fe94c6989a82c0d3afa9)
---
 lib/libc/gen/initgroups.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/lib/libc/gen/initgroups.c b/lib/libc/gen/initgroups.c
index b6697dd7ed8f..55f17a94fa8e 100644
--- a/lib/libc/gen/initgroups.c
+++ b/lib/libc/gen/initgroups.c
@@ -31,11 +31,7 @@
 
 #include <sys/param.h>
 
-#include "namespace.h"
-#include <err.h>
-#include "un-namespace.h"
 #include <errno.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 
@@ -51,12 +47,13 @@ initgroups(const char *uname, gid_t agroup)
 	 * setgroups to fail and set errno.
 	 */
 	ngroups_max = sysconf(_SC_NGROUPS_MAX) + 2;
-	if ((groups = malloc(sizeof(*groups) * ngroups_max)) == NULL)
-		return (ENOMEM);
+	groups = malloc(sizeof(*groups) * ngroups_max);
+	if (groups == NULL)
+		return (-1); /* malloc() set 'errno'. */
 
 	ngroups = (int)ngroups_max;
 	getgrouplist(uname, agroup, groups, &ngroups);
 	ret = setgroups(ngroups, groups);
 	free(groups);
-	return (ret);
+	return (ret); /* setgroups() set 'errno'. */
 }