Skip to content

Commit

Permalink
syscall: allow empty string argument to SetsockoptString
Browse files Browse the repository at this point in the history
Don't panic with "index out of range" on empty string argument.

Fixes #31277

Change-Id: I005f9523caec76337cb2ec87272a6be4736bce18
Reviewed-on: https://go-review.googlesource.com/c/go/+/170937
Run-TryBot: Tobias Klauser <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
tklauser committed Apr 5, 2019
1 parent cea4471 commit db0c524
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/syscall/syscall_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions src/syscall/syscall_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit db0c524

Please sign in to comment.