From ad97f365e15054527b467150f898ec66d7f81e14 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sat, 8 Dec 2018 18:04:56 +0100 Subject: [PATCH] unix: avoid index out of range in Vmsplice with empty iovs Passing an empty iovs []Iovec slice to Vmsplice leads to an index out of range panic. Fix this by passing an nil unsafe.Pointer to the underlying syscall in case of an empty slice. Change-Id: If1844c1b2eb0833de598aed7e79b9fcf061f7975 Reviewed-on: https://go-review.googlesource.com/c/153317 Run-TryBot: Tobias Klauser TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- unix/syscall_linux.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/unix/syscall_linux.go b/unix/syscall_linux.go index 466b2576..c2c7a000 100644 --- a/unix/syscall_linux.go +++ b/unix/syscall_linux.go @@ -1503,15 +1503,12 @@ func Munmap(b []byte) (err error) { // Vmsplice splices user pages from a slice of Iovecs into a pipe specified by fd, // using the specified flags. func Vmsplice(fd int, iovs []Iovec, flags int) (int, error) { - n, _, errno := Syscall6( - SYS_VMSPLICE, - uintptr(fd), - uintptr(unsafe.Pointer(&iovs[0])), - uintptr(len(iovs)), - uintptr(flags), - 0, - 0, - ) + var p unsafe.Pointer + if len(iovs) > 0 { + p = unsafe.Pointer(&iovs[0]) + } + + n, _, errno := Syscall6(SYS_VMSPLICE, uintptr(fd), uintptr(p), uintptr(len(iovs)), uintptr(flags), 0, 0) if errno != 0 { return 0, syscall.Errno(errno) }