]> Cypherpunks repositories - gostls13.git/commitdiff
net: never use backlog > 65535
authorRuss Cox <rsc@golang.org>
Tue, 12 Mar 2013 05:48:48 +0000 (01:48 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 12 Mar 2013 05:48:48 +0000 (01:48 -0400)
The system call takes an int, but the kernel stores it in a uint16.
At least one Linux system sets /proc/sys/net/core/somaxconn
to 262144, which ends up being 0 in the uint16. Avoid being tricked.

FreeBSD sources also store the backlog in a uint16.
Assume the problem is systemic and fix it everywhere.

Fixes #5030.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/7480046

src/pkg/net/sock_bsd.go
src/pkg/net/sock_linux.go
src/pkg/net/sock_windows.go

index 3205f94047900c1d6474ac27f5402f0e27bec480..d99349265ebed6eeb4e43c9374b4eec3815af81d 100644 (file)
@@ -27,5 +27,11 @@ func maxListenerBacklog() int {
        if n == 0 || err != nil {
                return syscall.SOMAXCONN
        }
+       // FreeBSD stores the backlog in a uint16, as does Linux.
+       // Assume the other BSDs do too. Truncate number to avoid wrapping.
+       // See issue 5030.
+       if n > 1<<16-1 {
+               n = 1<<16 - 1
+       }
        return int(n)
 }
index 8bbd74ddc92d1a8668cc92461103072d74371317..cc5ce153b368f5f2a380d3761c17bf4336addfd4 100644 (file)
@@ -21,5 +21,11 @@ func maxListenerBacklog() int {
        if n == 0 || !ok {
                return syscall.SOMAXCONN
        }
+       // Linux stores the backlog in a uint16.
+       // Truncate number to avoid wrapping.
+       // See issue 5030.
+       if n > 1<<16-1 {
+               n = 1<<16 - 1
+       }
        return n
 }
index a77c48437f22c739234536c034cd75a816b41cdf..41368d39e819ad6d0eba88f2d569d28631531638 100644 (file)
@@ -8,6 +8,7 @@ import "syscall"
 
 func maxListenerBacklog() int {
        // TODO: Implement this
+       // NOTE: Never return a number bigger than 1<<16 - 1. See issue 5030.
        return syscall.SOMAXCONN
 }