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

@@ -223,7 +223,7 @@ func TestReadDirN(t *testing.T) {
}{
{0, 0, 0},
{0, 1, 0},
{1, 0, 1},
{1, 0, 0},
{0, -1, 0},
{1, -1, 1},
{10, -1, 10},
@@ -236,19 +236,24 @@ func TestReadDirN(t *testing.T) {
for i, testCase := range testCases {
dir := mustSetupDir(t)
defer os.RemoveAll(dir)
for c := 1; c <= testCase.numFiles; c++ {
if err := ioutil.WriteFile(filepath.Join(dir, fmt.Sprintf("%d", c)), []byte{}, os.ModePerm); err != nil {
err := ioutil.WriteFile(filepath.Join(dir, fmt.Sprintf("%d", c)), []byte{}, os.ModePerm)
if err != nil {
os.RemoveAll(dir)
t.Fatalf("Unable to create a file, %s", err)
}
}
entries, err := readDirN(dir, testCase.n)
if err != nil {
os.RemoveAll(dir)
t.Fatalf("Unable to read entries, %s", err)
}
if len(entries) != testCase.expectedNum {
t.Fatalf("Test %d: unexpected number of entries, waiting for %d, but found %d", i+1, testCase.expectedNum, len(entries))
os.RemoveAll(dir)
t.Fatalf("Test %d: unexpected number of entries, waiting for %d, but found %d",
i+1, testCase.expectedNum, len(entries))
}
os.RemoveAll(dir)
}
}