aboutsummaryrefslogtreecommitdiffstats
path: root/community/go/default-sc-getpw-r-size-max.patch
blob: 77be27d13d4511578ba49826bea0be4c90bc9fc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
From cb867d2fd64adc851f82be3c6eb6e38ec008930b Mon Sep 17 00:00:00 2001
From: Dominik Honnef <dominik@honnef.co>
Date: Sun, 21 Jun 2015 20:07:29 +0200
Subject: [PATCH] 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 <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
---
 src/os/user/lookup_unix.go | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

diff --git a/src/os/user/lookup_unix.go b/src/os/user/lookup_unix.go
index f4f603e..e8a1eb1 100644
--- a/src/os/user/lookup_unix.go
+++ b/src/os/user/lookup_unix.go
@@ -9,7 +9,6 @@ package user
 
 import (
 	"fmt"
-	"runtime"
 	"strconv"
 	"strings"
 	"syscall"
@@ -55,17 +54,15 @@ func lookupUnix(uid int, username string, lookupByName bool) (*User, error) {
 	var pwd C.struct_passwd
 	var result *C.struct_passwd
 
-	var bufSize C.long
-	if runtime.GOOS == "dragonfly" || runtime.GOOS == "freebsd" {
-		// DragonFly and FreeBSD do not have _SC_GETPW_R_SIZE_MAX
-		// and just return -1.  So just use the same
-		// size that Linux returns.
+	bufSize := C.sysconf(C._SC_GETPW_R_SIZE_MAX)
+	if bufSize == -1 {
+		// DragonFly and FreeBSD do not have _SC_GETPW_R_SIZE_MAX.
+		// Additionally, not all Linux systems have it, either. For
+		// example, the musl libc returns -1.
 		bufSize = 1024
-	} else {
-		bufSize = C.sysconf(C._SC_GETPW_R_SIZE_MAX)
-		if bufSize <= 0 || bufSize > 1<<20 {
-			return nil, fmt.Errorf("user: unreasonable _SC_GETPW_R_SIZE_MAX of %d", bufSize)
-		}
+	}
+	if bufSize <= 0 || bufSize > 1<<20 {
+		return nil, fmt.Errorf("user: unreasonable _SC_GETPW_R_SIZE_MAX of %d", bufSize)
 	}
 	buf := C.malloc(C.size_t(bufSize))
 	defer C.free(buf)