Commit cb867d2f authored by Dominik Honnef's avatar Dominik Honnef Committed by Brad Fitzpatrick

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/13772Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
parent 7fb7f532
...@@ -9,7 +9,6 @@ package user ...@@ -9,7 +9,6 @@ package user
import ( import (
"fmt" "fmt"
"runtime"
"strconv" "strconv"
"strings" "strings"
"syscall" "syscall"
...@@ -55,17 +54,15 @@ func lookupUnix(uid int, username string, lookupByName bool) (*User, error) { ...@@ -55,17 +54,15 @@ func lookupUnix(uid int, username string, lookupByName bool) (*User, error) {
var pwd C.struct_passwd var pwd C.struct_passwd
var result *C.struct_passwd var result *C.struct_passwd
var bufSize C.long bufSize := C.sysconf(C._SC_GETPW_R_SIZE_MAX)
if runtime.GOOS == "dragonfly" || runtime.GOOS == "freebsd" { if bufSize == -1 {
// DragonFly and FreeBSD do not have _SC_GETPW_R_SIZE_MAX // DragonFly and FreeBSD do not have _SC_GETPW_R_SIZE_MAX.
// and just return -1. So just use the same // Additionally, not all Linux systems have it, either. For
// size that Linux returns. // example, the musl libc returns -1.
bufSize = 1024 bufSize = 1024
} else { }
bufSize = C.sysconf(C._SC_GETPW_R_SIZE_MAX) if bufSize <= 0 || bufSize > 1<<20 {
if bufSize <= 0 || bufSize > 1<<20 { return nil, fmt.Errorf("user: unreasonable _SC_GETPW_R_SIZE_MAX of %d", bufSize)
return nil, fmt.Errorf("user: unreasonable _SC_GETPW_R_SIZE_MAX of %d", bufSize)
}
} }
buf := C.malloc(C.size_t(bufSize)) buf := C.malloc(C.size_t(bufSize))
defer C.free(buf) defer C.free(buf)
......
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