Skip to content

Commit cb867d2

Browse files
dominikhbradfitz
authored andcommitted
os/user: don't depend on _SC_GETPW_R_SIZE_MAX on Linux
Even Linux systems may not have _SC_GETPW_R_SIZE_MAX if using a different libc than glibc (e.g. musl). Instead of having special-cases for the BSDs, handle -1 correctly by always using a default buffer size. Fixes #11319. Change-Id: I8b1b260eb9830e6dbe7667f3f33d115ae4de4ce8 Reviewed-on: https://go-review.googlesource.com/13772 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]>
1 parent 7fb7f53 commit cb867d2

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/os/user/lookup_unix.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package user
99

1010
import (
1111
"fmt"
12-
"runtime"
1312
"strconv"
1413
"strings"
1514
"syscall"
@@ -55,17 +54,15 @@ func lookupUnix(uid int, username string, lookupByName bool) (*User, error) {
5554
var pwd C.struct_passwd
5655
var result *C.struct_passwd
5756

58-
var bufSize C.long
59-
if runtime.GOOS == "dragonfly" || runtime.GOOS == "freebsd" {
60-
// DragonFly and FreeBSD do not have _SC_GETPW_R_SIZE_MAX
61-
// and just return -1. So just use the same
62-
// size that Linux returns.
57+
bufSize := C.sysconf(C._SC_GETPW_R_SIZE_MAX)
58+
if bufSize == -1 {
59+
// DragonFly and FreeBSD do not have _SC_GETPW_R_SIZE_MAX.
60+
// Additionally, not all Linux systems have it, either. For
61+
// example, the musl libc returns -1.
6362
bufSize = 1024
64-
} else {
65-
bufSize = C.sysconf(C._SC_GETPW_R_SIZE_MAX)
66-
if bufSize <= 0 || bufSize > 1<<20 {
67-
return nil, fmt.Errorf("user: unreasonable _SC_GETPW_R_SIZE_MAX of %d", bufSize)
68-
}
63+
}
64+
if bufSize <= 0 || bufSize > 1<<20 {
65+
return nil, fmt.Errorf("user: unreasonable _SC_GETPW_R_SIZE_MAX of %d", bufSize)
6966
}
7067
buf := C.malloc(C.size_t(bufSize))
7168
defer C.free(buf)

0 commit comments

Comments
 (0)