Commit 7edb721f authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

os/user: handle large 32-bit uid/gid values when stringifying User.Uid/Gid

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: default avatarIan Lance Taylor <iant@golang.org>
parent 92c35824
...@@ -114,8 +114,8 @@ func lookupUnixUid(uid int) (*User, error) { ...@@ -114,8 +114,8 @@ func lookupUnixUid(uid int) (*User, error) {
func buildUser(pwd *C.struct_passwd) *User { func buildUser(pwd *C.struct_passwd) *User {
u := &User{ u := &User{
Uid: strconv.Itoa(int(pwd.pw_uid)), Uid: strconv.FormatUint(uint64(pwd.pw_uid), 10),
Gid: strconv.Itoa(int(pwd.pw_gid)), Gid: strconv.FormatUint(uint64(pwd.pw_gid), 10),
Username: C.GoString(pwd.pw_name), Username: C.GoString(pwd.pw_name),
Name: C.GoString(pwd.pw_gecos), Name: C.GoString(pwd.pw_gecos),
HomeDir: C.GoString(pwd.pw_dir), HomeDir: C.GoString(pwd.pw_dir),
...@@ -269,3 +269,11 @@ const maxBufferSize = 1 << 20 ...@@ -269,3 +269,11 @@ const maxBufferSize = 1 << 20
func isSizeReasonable(sz int64) bool { func isSizeReasonable(sz int64) bool {
return sz > 0 && sz <= maxBufferSize 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
}
// 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)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment