|
| 1 | +From 0c15fbcf18c0736149d06957e9058563fec065d3 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Victor Moene <victor.moene@northern.tech> |
| 3 | +Date: Tue, 25 Aug 2026 09:47:54 +0200 |
| 4 | +Subject: [PATCH] Guard <pwd.h>/<grp.h> includes in idcache.c and userspec.c |
| 5 | + |
| 6 | +We only build the "date" program out of coreutils, but the build compiles |
| 7 | +every gnulib lib/*.c file into a single lib/libcoreutils.a used by all |
| 8 | +coreutils programs, regardless of whether "date" actually needs them. |
| 9 | + |
| 10 | +lib/idcache.c and lib/userspec.c unconditionally include <pwd.h> and |
| 11 | +<grp.h> to resolve uids/gids via the system's user and group databases. |
| 12 | +Native Windows (mingw-w64) ships neither header, so compiling |
| 13 | +lib/libcoreutils.a for date.exe failed there even though date.exe never |
| 14 | +references any symbol from these two files. |
| 15 | + |
| 16 | +Guard the includes, and the function bodies that depend on them, with |
| 17 | +the HAVE_PWD_H/HAVE_GRP_H macros that configure already defines via |
| 18 | +AC_CHECK_HEADERS. This way the two files compile down to empty |
| 19 | +translation units on platforms lacking these headers. If some other |
| 20 | +coreutils program actually needs the passwd/group lookups these files |
| 21 | +provide, linking that program will now fail with an undefined |
| 22 | +reference, surfacing the problem at build time instead of silently |
| 23 | +linking in stub declarations that could never work. |
| 24 | + |
| 25 | +Ticket: None |
| 26 | +Changelog: none |
| 27 | +--- |
| 28 | + lib/idcache.c | 18 ++++++++++++++++-- |
| 29 | + lib/userspec.c | 18 ++++++++++++++++-- |
| 30 | + 2 files changed, 32 insertions(+), 4 deletions(-) |
| 31 | + |
| 32 | +diff --git a/lib/idcache.c b/lib/idcache.c |
| 33 | +index 5fd5f2c..327175e 100644 |
| 34 | +--- a/lib/idcache.c |
| 35 | ++++ b/lib/idcache.c |
| 36 | +@@ -22,8 +22,12 @@ |
| 37 | + #include <stddef.h> |
| 38 | + #include <stdio.h> |
| 39 | + #include <string.h> |
| 40 | +-#include <pwd.h> |
| 41 | +-#include <grp.h> |
| 42 | ++#if HAVE_PWD_H |
| 43 | ++# include <pwd.h> |
| 44 | ++#endif |
| 45 | ++#if HAVE_GRP_H |
| 46 | ++# include <grp.h> |
| 47 | ++#endif |
| 48 | + |
| 49 | + #include <unistd.h> |
| 50 | + |
| 51 | +@@ -63,6 +67,14 @@ static struct userid *group_alist; |
| 52 | + /* Each entry on list is a group name for which the first lookup failed. */ |
| 53 | + static struct userid *nogroup_alist; |
| 54 | + |
| 55 | ++/* The functions below all resolve uids/gids via the system's user and |
| 56 | ++ group databases, so they require <pwd.h> and <grp.h>. On platforms |
| 57 | ++ that lack these headers (e.g. native Windows), omit them entirely |
| 58 | ++ rather than fake up the lookups: if some program actually needs them, |
| 59 | ++ it will fail to link, which is preferable to silently linking in a |
| 60 | ++ passwd/group lookup that can never work. */ |
| 61 | ++#if HAVE_PWD_H && HAVE_GRP_H |
| 62 | ++ |
| 63 | + /* Translate UID to a login name, with cache, or NULL if unresolved. */ |
| 64 | + |
| 65 | + char * |
| 66 | +@@ -220,3 +232,5 @@ getgidbyname (const char *group) |
| 67 | + nogroup_alist = tail; |
| 68 | + return NULL; |
| 69 | + } |
| 70 | ++ |
| 71 | ++#endif /* HAVE_PWD_H && HAVE_GRP_H */ |
| 72 | +diff --git a/lib/userspec.c b/lib/userspec.c |
| 73 | +index 57cd023..943257a 100644 |
| 74 | +--- a/lib/userspec.c |
| 75 | ++++ b/lib/userspec.c |
| 76 | +@@ -24,8 +24,12 @@ |
| 77 | + |
| 78 | + #include <stdio.h> |
| 79 | + #include <sys/types.h> |
| 80 | +-#include <pwd.h> |
| 81 | +-#include <grp.h> |
| 82 | ++#if HAVE_PWD_H |
| 83 | ++# include <pwd.h> |
| 84 | ++#endif |
| 85 | ++#if HAVE_GRP_H |
| 86 | ++# include <grp.h> |
| 87 | ++#endif |
| 88 | + |
| 89 | + #if HAVE_SYS_PARAM_H |
| 90 | + # include <sys/param.h> |
| 91 | +@@ -97,6 +101,14 @@ is_number (const char *str) |
| 92 | + } |
| 93 | + #endif |
| 94 | + |
| 95 | ++/* Resolving a user/group spec requires looking it up via the system's |
| 96 | ++ user and group databases, so the code below needs <pwd.h> and |
| 97 | ++ <grp.h>. On platforms that lack these headers (e.g. native Windows), |
| 98 | ++ omit it entirely rather than fake up the lookups: if some program |
| 99 | ++ actually needs it, it will fail to link, which is preferable to |
| 100 | ++ silently linking in a passwd/group lookup that can never work. */ |
| 101 | ++#if HAVE_PWD_H && HAVE_GRP_H |
| 102 | ++ |
| 103 | + static char const * |
| 104 | + parse_with_separator (char const *spec, char const *separator, |
| 105 | + uid_t *uid, gid_t *gid, |
| 106 | +@@ -289,6 +301,8 @@ parse_user_spec (char const *spec, uid_t *uid, gid_t *gid, |
| 107 | + return parse_user_spec_warn (spec, uid, gid, username, groupname, NULL); |
| 108 | + } |
| 109 | + |
| 110 | ++#endif /* HAVE_PWD_H && HAVE_GRP_H */ |
| 111 | ++ |
| 112 | + #ifdef TEST |
| 113 | + |
| 114 | + # define NULL_CHECK(s) ((s) == NULL ? "(null)" : (s)) |
| 115 | +-- |
| 116 | +2.43.0 |
0 commit comments