]> Cypherpunks repositories - gostls13.git/commitdiff
os/user: retrieve Current username from /etc/passwd, not $USER
authorKevin Burke <kev@inburke.com>
Sat, 8 Sep 2018 19:35:03 +0000 (12:35 -0700)
committerKevin Burke <kev@inburke.com>
Sun, 9 Sep 2018 19:24:00 +0000 (19:24 +0000)
Per golang/go#27524 there are situations where the username for the
uid does not match the value in the $USER environment variable and it
seems sensible to choose the value in /etc/passwd when they disagree.

This may make the Current() call slightly more expensive, since we
read /etc/passwd with cgo disabled instead of just checking the
environment. However, we cache the result of Current() calls, so we
only invoke this cost once in the lifetime of the process.

Fixes #14626.
Fixes #27524.
Updates #24884.

Change-Id: I0dcd224cf7f61dd5292f3fcc363aa2e9656a2cb1
Reviewed-on: https://go-review.googlesource.com/134218
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/os/user/lookup_stubs.go
src/os/user/user_test.go

index f7d138ff46801d22fdb57da398cd2b39a09f3bd2..9fc03c65d95ac470436a50bae802942281dc727e 100644 (file)
@@ -19,8 +19,15 @@ func init() {
 }
 
 func current() (*User, error) {
-       u := &User{
-               Uid:      currentUID(),
+       uid := currentUID()
+       // $USER and /etc/passwd may disagree; prefer the latter if we can get it.
+       // See issue 27524 for more information.
+       u, err := lookupUserId(uid)
+       if err == nil {
+               return u, nil
+       }
+       u = &User{
+               Uid:      uid,
                Gid:      currentGID(),
                Username: os.Getenv("USER"),
                Name:     "", // ignored
index 8fd760e64981be6fc20fd150fd9b50ff48796c2a..2563077eb2e97d6274bc6c1c7fb21fe45b1b88e9 100644 (file)
@@ -5,33 +5,18 @@
 package user
 
 import (
-       "internal/testenv"
-       "os"
        "runtime"
        "testing"
 )
 
 func checkUser(t *testing.T) {
+       t.Helper()
        if !userImplemented {
                t.Skip("user: not implemented; skipping tests")
        }
 }
 
 func TestCurrent(t *testing.T) {
-       // The Go builders (in particular the ones using containers)
-       // often have minimal environments without $HOME or $USER set,
-       // which breaks Current which relies on those working as a
-       // fallback.
-       // TODO: we should fix that (Issue 24884) and remove these
-       // workarounds.
-       if testenv.Builder() != "" && runtime.GOOS != "windows" && runtime.GOOS != "plan9" {
-               if os.Getenv("HOME") == "" {
-                       os.Setenv("HOME", "/tmp")
-               }
-               if os.Getenv("USER") == "" {
-                       os.Setenv("USER", "gobuilder")
-               }
-       }
        u, err := Current()
        if err != nil {
                t.Fatalf("Current: %v (got %#v)", err, u)
@@ -108,6 +93,7 @@ func TestLookupId(t *testing.T) {
 }
 
 func checkGroup(t *testing.T) {
+       t.Helper()
        if !groupImplemented {
                t.Skip("user: group not implemented; skipping test")
        }