Skip to content

Commit aed1f92

Browse files
benhoytgopherbot
authored andcommitted
os/user: make Lookup* functions properly handle ENOENT
The libc docs say that these functions can return ENOENT, and per issue #67912, this does happen in practice sometimes. Handle both the ENOENT and !found cases the same way, for Lookup, LookupId, LookupGroup, LookupGroupId. Fixes #67912 Change-Id: I993935af44c83ad785b6cd735fc313a3647daa19 Reviewed-on: https://go-review.googlesource.com/c/go/+/591555 Reviewed-by: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent 5d85004 commit aed1f92

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/os/user/cgo_lookup_unix.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ func lookupUser(username string) (*User, error) {
3131
(*_C_char)(unsafe.Pointer(&buf[0])), _C_size_t(len(buf)))
3232
return errno
3333
})
34+
if err == syscall.ENOENT || (err == nil && !found) {
35+
return nil, UnknownUserError(username)
36+
}
3437
if err != nil {
3538
return nil, fmt.Errorf("user: lookup username %s: %v", username, err)
3639
}
37-
if !found {
38-
return nil, UnknownUserError(username)
39-
}
4040
return buildUser(&pwd), err
4141
}
4242

@@ -58,12 +58,12 @@ func lookupUnixUid(uid int) (*User, error) {
5858
(*_C_char)(unsafe.Pointer(&buf[0])), _C_size_t(len(buf)))
5959
return errno
6060
})
61+
if err == syscall.ENOENT || (err == nil && !found) {
62+
return nil, UnknownUserIdError(uid)
63+
}
6164
if err != nil {
6265
return nil, fmt.Errorf("user: lookup userid %d: %v", uid, err)
6366
}
64-
if !found {
65-
return nil, UnknownUserIdError(uid)
66-
}
6767
return buildUser(&pwd), nil
6868
}
6969

@@ -96,12 +96,12 @@ func lookupGroup(groupname string) (*Group, error) {
9696
(*_C_char)(unsafe.Pointer(&buf[0])), _C_size_t(len(buf)))
9797
return errno
9898
})
99+
if err == syscall.ENOENT || (err == nil && !found) {
100+
return nil, UnknownGroupError(groupname)
101+
}
99102
if err != nil {
100103
return nil, fmt.Errorf("user: lookup groupname %s: %v", groupname, err)
101104
}
102-
if !found {
103-
return nil, UnknownGroupError(groupname)
104-
}
105105
return buildGroup(&grp), nil
106106
}
107107

@@ -123,12 +123,12 @@ func lookupUnixGid(gid int) (*Group, error) {
123123
(*_C_char)(unsafe.Pointer(&buf[0])), _C_size_t(len(buf)))
124124
return syscall.Errno(errno)
125125
})
126+
if err == syscall.ENOENT || (err == nil && !found) {
127+
return nil, UnknownGroupIdError(strconv.Itoa(gid))
128+
}
126129
if err != nil {
127130
return nil, fmt.Errorf("user: lookup groupid %d: %v", gid, err)
128131
}
129-
if !found {
130-
return nil, UnknownGroupIdError(strconv.Itoa(gid))
131-
}
132132
return buildGroup(&grp), nil
133133
}
134134

0 commit comments

Comments
 (0)