Skip to content

Commit e54d0f5

Browse files
committed
Merge branch 'jk/ident-loosen-getpwuid' into maint
When getpwuid() on the system returned NULL (e.g. the user is not in the /etc/passwd file or other uid-to-name mappings), the codepath to find who the user is to record it in the reflog barfed and died. Loosen the check in this codepath, which already accepts questionable ident string (e.g. host part of the e-mail address is obviously bogus), and in general when we operate fmt_ident() function in non-strict mode. * jk/ident-loosen-getpwuid: ident: loosen getpwuid error in non-strict mode ident: keep a flag for bogus default_email ident: make xgetpwuid_self() a static local helper
2 parents 06b5c93 + 92bcbb9 commit e54d0f5

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

git-compat-util.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -923,9 +923,6 @@ int access_or_die(const char *path, int mode, unsigned flag);
923923
/* Warn on an inaccessible file that ought to be accessible */
924924
void warn_on_inaccessible(const char *path);
925925

926-
/* Get the passwd entry for the UID of the current process. */
927-
struct passwd *xgetpwuid_self(void);
928-
929926
#ifdef GMTIME_UNRELIABLE_ERRORS
930927
struct tm *git_gmtime(const time_t *);
931928
struct tm *git_gmtime_r(const time_t *, struct tm *);

ident.c

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
static struct strbuf git_default_name = STRBUF_INIT;
1111
static struct strbuf git_default_email = STRBUF_INIT;
1212
static struct strbuf git_default_date = STRBUF_INIT;
13+
static int default_email_is_bogus;
14+
static int default_name_is_bogus;
1315

1416
#define IDENT_NAME_GIVEN 01
1517
#define IDENT_MAIL_GIVEN 02
@@ -23,6 +25,25 @@ static int author_ident_explicitly_given;
2325
#define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
2426
#endif
2527

28+
static struct passwd *xgetpwuid_self(int *is_bogus)
29+
{
30+
struct passwd *pw;
31+
32+
errno = 0;
33+
pw = getpwuid(getuid());
34+
if (!pw) {
35+
static struct passwd fallback;
36+
fallback.pw_name = "unknown";
37+
#ifndef NO_GECOS_IN_PWENT
38+
fallback.pw_gecos = "Unknown";
39+
#endif
40+
pw = &fallback;
41+
if (is_bogus)
42+
*is_bogus = 1;
43+
}
44+
return pw;
45+
}
46+
2647
static void copy_gecos(const struct passwd *w, struct strbuf *name)
2748
{
2849
char *src;
@@ -96,22 +117,26 @@ static int canonical_name(const char *host, struct strbuf *out)
96117
return status;
97118
}
98119

99-
static void add_domainname(struct strbuf *out)
120+
static void add_domainname(struct strbuf *out, int *is_bogus)
100121
{
101122
char buf[1024];
102123

103124
if (gethostname(buf, sizeof(buf))) {
104125
warning("cannot get host name: %s", strerror(errno));
105126
strbuf_addstr(out, "(none)");
127+
*is_bogus = 1;
106128
return;
107129
}
108130
if (strchr(buf, '.'))
109131
strbuf_addstr(out, buf);
110-
else if (canonical_name(buf, out) < 0)
132+
else if (canonical_name(buf, out) < 0) {
111133
strbuf_addf(out, "%s.(none)", buf);
134+
*is_bogus = 1;
135+
}
112136
}
113137

114-
static void copy_email(const struct passwd *pw, struct strbuf *email)
138+
static void copy_email(const struct passwd *pw, struct strbuf *email,
139+
int *is_bogus)
115140
{
116141
/*
117142
* Make up a fake email address
@@ -122,13 +147,13 @@ static void copy_email(const struct passwd *pw, struct strbuf *email)
122147

123148
if (!add_mailname_host(email))
124149
return; /* read from "/etc/mailname" (Debian) */
125-
add_domainname(email);
150+
add_domainname(email, is_bogus);
126151
}
127152

128153
const char *ident_default_name(void)
129154
{
130155
if (!git_default_name.len) {
131-
copy_gecos(xgetpwuid_self(), &git_default_name);
156+
copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
132157
strbuf_trim(&git_default_name);
133158
}
134159
return git_default_name.buf;
@@ -144,7 +169,8 @@ const char *ident_default_email(void)
144169
committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
145170
author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
146171
} else
147-
copy_email(xgetpwuid_self(), &git_default_email);
172+
copy_email(xgetpwuid_self(&default_email_is_bogus),
173+
&git_default_email, &default_email_is_bogus);
148174
strbuf_trim(&git_default_email);
149175
}
150176
return git_default_email.buf;
@@ -332,12 +358,17 @@ const char *fmt_ident(const char *name, const char *email,
332358
fputs(env_hint, stderr);
333359
die("empty ident name (for <%s>) not allowed", email);
334360
}
335-
pw = xgetpwuid_self();
361+
pw = xgetpwuid_self(NULL);
336362
name = pw->pw_name;
337363
}
338364

339-
if (strict && email == git_default_email.buf &&
340-
strstr(email, "(none)")) {
365+
if (want_name && strict &&
366+
name == git_default_name.buf && default_name_is_bogus) {
367+
fputs(env_hint, stderr);
368+
die("unable to auto-detect name (got '%s')", name);
369+
}
370+
371+
if (strict && email == git_default_email.buf && default_email_is_bogus) {
341372
fputs(env_hint, stderr);
342373
die("unable to auto-detect email address (got '%s')", email);
343374
}

wrapper.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -601,18 +601,6 @@ int access_or_die(const char *path, int mode, unsigned flag)
601601
return ret;
602602
}
603603

604-
struct passwd *xgetpwuid_self(void)
605-
{
606-
struct passwd *pw;
607-
608-
errno = 0;
609-
pw = getpwuid(getuid());
610-
if (!pw)
611-
die(_("unable to look up current user in the passwd file: %s"),
612-
errno ? strerror(errno) : _("no such user"));
613-
return pw;
614-
}
615-
616604
char *xgetcwd(void)
617605
{
618606
struct strbuf sb = STRBUF_INIT;

0 commit comments

Comments
 (0)