Safely use unsafe.Pointer to avoid crashes on ARM (#8027)

Refactor the Dirent parsing code such that when we
calculate offsets are correct based on the platform
This PR fixes a silent potential crash on ARM
architecture.
This commit is contained in:
Harshavardhana
2019-08-09 08:54:11 -07:00
committed by GitHub
parent 43c72374d4
commit a8296445ad
7 changed files with 172 additions and 128 deletions

View File

@@ -57,9 +57,7 @@ func readDirN(dirPath string, count int) (entries []string, err error) {
data := &syscall.Win32finddata{}
remaining := count
done := false
for !done {
for count != 0 {
e := syscall.FindNextFile(syscall.Handle(d.Fd()), data)
if e != nil {
if e == syscall.ERROR_NO_MORE_FILES {
@@ -74,7 +72,7 @@ func readDirN(dirPath string, count int) (entries []string, err error) {
}
}
name := syscall.UTF16ToString(data.FileName[0:])
if name == "." || name == ".." { // Useless names
if name == "" || name == "." || name == ".." { // Useless names
continue
}
switch {
@@ -101,10 +99,7 @@ func readDirN(dirPath string, count int) (entries []string, err error) {
default:
entries = append(entries, name)
}
if remaining > 0 {
remaining--
done = remaining == 0
}
count--
}
return entries, nil
}