Skip to content

Commit

Permalink
Merge pull request twpayne#634 from twpayne/ignore-lookupgroupid-errors
Browse files Browse the repository at this point in the history
Only set group value if user.LookupGroupId succeeds
  • Loading branch information
twpayne committed Mar 21, 2020
2 parents d0ab459 + d22a3cf commit db03a2b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
5 changes: 0 additions & 5 deletions cmd/cgo.go

This file was deleted.

22 changes: 11 additions & 11 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,20 +317,20 @@ func (c *Config) getDefaultData() (map[string]interface{}, error) {
}
data["username"] = currentUser.Username

// user.LookupGroupId looks up a group by gid. If CGO is enabled, then this
// uses an underlying C library call (e.g. getgrgid_r on Linux) and is
// trustworthy. If CGO is disabled then the fallback implementation only
// searches /etc/group, which is typically empty if an external directory
// service is being used, and so the lookup fails. So, if
// user.LookupGroupId returns an error, only return an error if CGO is
// enabled.
// user.LookupGroupId is generally unreliable:
//
// If CGO is enabled, then this uses an underlying C library call (e.g.
// getgrgid_r on Linux) and is trustworthy, except on recent versions of Go
// on Android, where LookupGroupId is not implemented.
//
// If CGO is disabled then the fallback implementation only searches
// /etc/group, which is typically empty if an external directory service is
// being used, and so the lookup fails.
//
// So, only set group if user.LookupGroupId does not return an error.
group, err := user.LookupGroupId(currentUser.Gid)
if err == nil {
data["group"] = group.Name
} else if cgoEnabled && runtime.GOOS != "windows" {
// Only return an error if CGO is enabled and the platform is
// non-Windows (groups don't really mean much on Windows).
return nil, err
}

homedir, err := os.UserHomeDir()
Expand Down
5 changes: 0 additions & 5 deletions cmd/nocgo.go

This file was deleted.

0 comments on commit db03a2b

Please sign in to comment.