1
0
mirror of https://github.com/golang/go synced 2024-11-23 09:30:03 -07:00

syscall: swap src, dest arguments in convertFromDirents11 for FreeBSD

make fixedSize, oldFixedSize constants.
use st instead of stat for function arg so that we do not shadow the stat() function.

dstPos+reclen == len(buf) is a valid write location, update the break condition.

Change-Id: I55f9210f54d24a3f9cda1ebab52437436254f8f4
Reviewed-on: https://go-review.googlesource.com/c/143637
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Yuval Pavel Zholkover 2018-10-20 18:54:07 +03:00 committed by Brad Fitzpatrick
parent eef0140137
commit 5a7cfbc011
3 changed files with 82 additions and 13 deletions

View File

@ -0,0 +1,12 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package syscall
type Dirent_freebsd11 = dirent_freebsd11
var (
Roundup = roundup
ConvertFromDirents11 = convertFromDirents11
)

View File

@ -223,31 +223,31 @@ func Fstat(fd int, st *Stat_t) (err error) {
return nil
}
func Statfs(path string, stat *Statfs_t) (err error) {
func Statfs(path string, st *Statfs_t) (err error) {
var oldStatfs statfs_freebsd11_t
if supportsABI(_ino64First) {
return statfs_freebsd12(path, stat)
return statfs_freebsd12(path, st)
}
err = statfs(path, &oldStatfs)
if err != nil {
return err
}
stat.convertFrom(&oldStatfs)
st.convertFrom(&oldStatfs)
return nil
}
func Fstatfs(fd int, stat *Statfs_t) (err error) {
func Fstatfs(fd int, st *Statfs_t) (err error) {
var oldStatfs statfs_freebsd11_t
if supportsABI(_ino64First) {
return fstatfs_freebsd12(fd, stat)
return fstatfs_freebsd12(fd, st)
}
err = fstatfs(fd, &oldStatfs)
if err != nil {
return err
}
stat.convertFrom(&oldStatfs)
st.convertFrom(&oldStatfs)
return nil
}
@ -262,7 +262,7 @@ func Getdirentries(fd int, buf []byte, basep *uintptr) (n int, err error) {
oldBuf := make([]byte, oldBufLen)
n, err = getdirentries(fd, oldBuf, basep)
if err == nil && n > 0 {
n = convertFromDirents11(oldBuf[:n], buf)
n = convertFromDirents11(buf, oldBuf[:n])
}
return
}
@ -344,17 +344,20 @@ func (s *Statfs_t) convertFrom(old *statfs_freebsd11_t) {
copy(s.Mntonname[:], old.Mntonname[:n])
}
func convertFromDirents11(old []byte, buf []byte) int {
oldFixedSize := int(unsafe.Offsetof((*dirent_freebsd11)(nil).Name))
fixedSize := int(unsafe.Offsetof((*Dirent)(nil).Name))
srcPos := 0
func convertFromDirents11(buf []byte, old []byte) int {
const (
fixedSize = int(unsafe.Offsetof(Dirent{}.Name))
oldFixedSize = int(unsafe.Offsetof(dirent_freebsd11{}.Name))
)
dstPos := 0
srcPos := 0
for dstPos+fixedSize < len(buf) && srcPos+oldFixedSize < len(old) {
srcDirent := (*dirent_freebsd11)(unsafe.Pointer(&old[srcPos]))
dstDirent := (*Dirent)(unsafe.Pointer(&buf[dstPos]))
srcDirent := (*dirent_freebsd11)(unsafe.Pointer(&old[srcPos]))
reclen := roundup(fixedSize+int(srcDirent.Namlen)+1, 8)
if dstPos+reclen >= len(buf) {
if dstPos+reclen > len(buf) {
break
}

View File

@ -0,0 +1,54 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build freebsd
package syscall_test
import (
"fmt"
"syscall"
"testing"
"unsafe"
)
func TestConvertFromDirent11(t *testing.T) {
const (
filenameFmt = "%04d"
numFiles = 64
fixedHdrSize = int(unsafe.Offsetof(syscall.Dirent_freebsd11{}.Name))
)
namlen := len(fmt.Sprintf(filenameFmt, 0))
reclen := syscall.Roundup(fixedHdrSize+namlen+1, 4)
old := make([]byte, numFiles*reclen)
for i := 0; i < numFiles; i++ {
dent := syscall.Dirent_freebsd11{
Fileno: uint32(i + 1),
Reclen: uint16(reclen),
Type: syscall.DT_REG,
Namlen: uint8(namlen),
}
rec := make([]byte, reclen)
copy(rec, (*[fixedHdrSize]byte)(unsafe.Pointer(&dent))[:])
copy(rec[fixedHdrSize:], fmt.Sprintf(filenameFmt, i+1))
copy(old[i*reclen:], rec)
}
buf := make([]byte, 2*len(old))
n := syscall.ConvertFromDirents11(buf, old)
names := make([]string, 0, numFiles)
_, _, names = syscall.ParseDirent(buf[:n], -1, names)
if len(names) != numFiles {
t.Errorf("expected %d files, have %d; names: %q", numFiles, len(names), names)
}
for i, name := range names {
if expected := fmt.Sprintf(filenameFmt, i+1); name != expected {
t.Errorf("expected names[%d] to be %q; got %q", i, expected, name)
}
}
}