]> Cypherpunks repositories - gostls13.git/commitdiff
os/user: handle large 32-bit uid/gid values when stringifying User.Uid/Gid
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 15 Nov 2017 17:30:53 +0000 (17:30 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 16 Nov 2017 18:42:02 +0000 (18:42 +0000)
Fixes #22739

Change-Id: I374c29d237c498c9e5ac848b01f6d49d7c41b31f
Reviewed-on: https://go-review.googlesource.com/77930
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/os/user/cgo_lookup_unix.go
src/os/user/cgo_unix_test.go [new file with mode: 0644]

index 6f66851bbbc9c8c0818f51e71a68b65f6e172d89..987a2d8c7d16a85a0b7c93af1a6d8580c7cc5c53 100644 (file)
@@ -114,8 +114,8 @@ func lookupUnixUid(uid int) (*User, error) {
 
 func buildUser(pwd *C.struct_passwd) *User {
        u := &User{
-               Uid:      strconv.Itoa(int(pwd.pw_uid)),
-               Gid:      strconv.Itoa(int(pwd.pw_gid)),
+               Uid:      strconv.FormatUint(uint64(pwd.pw_uid), 10),
+               Gid:      strconv.FormatUint(uint64(pwd.pw_gid), 10),
                Username: C.GoString(pwd.pw_name),
                Name:     C.GoString(pwd.pw_gecos),
                HomeDir:  C.GoString(pwd.pw_dir),
@@ -269,3 +269,11 @@ const maxBufferSize = 1 << 20
 func isSizeReasonable(sz int64) bool {
        return sz > 0 && sz <= maxBufferSize
 }
+
+// Because we can't use cgo in tests:
+func structPasswdForNegativeTest() C.struct_passwd {
+       sp := C.struct_passwd{}
+       sp.pw_uid = 1<<32 - 2
+       sp.pw_gid = 1<<32 - 3
+       return sp
+}
diff --git a/src/os/user/cgo_unix_test.go b/src/os/user/cgo_unix_test.go
new file mode 100644 (file)
index 0000000..6741118
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build darwin dragonfly freebsd !android,linux netbsd openbsd solaris
+// +build cgo
+
+package user
+
+import (
+       "testing"
+)
+
+// Issue 22739
+func TestNegativeUid(t *testing.T) {
+       sp := structPasswdForNegativeTest()
+       u := buildUser(&sp)
+       if g, w := u.Uid, "4294967294"; g != w {
+               t.Errorf("Uid = %q; want %q", g, w)
+       }
+       if g, w := u.Gid, "4294967293"; g != w {
+               t.Errorf("Gid = %q; want %q", g, w)
+       }
+}