diff options
| author | Tobias Klauser <tklauser@distanz.ch> | 2019-04-05 17:10:40 +0200 |
|---|---|---|
| committer | Tobias Klauser <tobias.klauser@gmail.com> | 2019-04-05 15:42:17 +0000 |
| commit | db0c524211d82af1f632b78c80d22d734c8b1be2 (patch) | |
| tree | 5c83965ba8d13c895f3bd2caa4bc5081390ab0c6 /src | |
| parent | cea44714fb4e79b939e5b781ee61e97b3d7e1c14 (diff) | |
| download | go-db0c524211d82af1f632b78c80d22d734c8b1be2.tar.xz | |
syscall: allow empty string argument to SetsockoptString
Don't panic with "index out of range" on empty string argument.
Fixes golang/go#31277
Change-Id: I005f9523caec76337cb2ec87272a6be4736bce18
Reviewed-on: https://go-review.googlesource.com/c/go/+/170937
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src')
| -rw-r--r-- | src/syscall/syscall_unix.go | 6 | ||||
| -rw-r--r-- | src/syscall/syscall_unix_test.go | 8 |
2 files changed, 13 insertions, 1 deletions
diff --git a/src/syscall/syscall_unix.go b/src/syscall/syscall_unix.go index fd54dc0dc7..f73f55462a 100644 --- a/src/syscall/syscall_unix.go +++ b/src/syscall/syscall_unix.go @@ -323,7 +323,11 @@ func SetsockoptLinger(fd, level, opt int, l *Linger) (err error) { } func SetsockoptString(fd, level, opt int, s string) (err error) { - return setsockopt(fd, level, opt, unsafe.Pointer(&[]byte(s)[0]), uintptr(len(s))) + var p unsafe.Pointer + if len(s) > 0 { + p = unsafe.Pointer(&[]byte(s)[0]) + } + return setsockopt(fd, level, opt, p, uintptr(len(s))) } func SetsockoptTimeval(fd, level, opt int, tv *Timeval) (err error) { diff --git a/src/syscall/syscall_unix_test.go b/src/syscall/syscall_unix_test.go index 085afb2941..1a2c41dacd 100644 --- a/src/syscall/syscall_unix_test.go +++ b/src/syscall/syscall_unix_test.go @@ -355,3 +355,11 @@ func TestSeekFailure(t *testing.T) { t.Fatalf("Seek(-1, 0, 0) return error with empty message") } } + +func TestSetsockoptString(t *testing.T) { + // should not panic on empty string, see issue #31277 + err := syscall.SetsockoptString(-1, 0, 0, "") + if err == nil { + t.Fatalf("SetsockoptString: did not fail") + } +} |
