afero icon indicating copy to clipboard operation
afero copied to clipboard

Fix the infinite recursion with afero.Walk

Open erstam opened this issue 1 year ago • 6 comments

This PR fixes https://github.com/spf13/afero/issues/185

Fix the infinite recursion by skipping the current directory in the directory names listing

erstam avatar Nov 14 '23 17:11 erstam

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Nov 14 '23 17:11 CLAassistant

@spf13 Please, who can review this PR ?

erstam avatar Nov 15 '23 18:11 erstam

@erstam this needs a simple test (that does infinite recursion without this patch).

bep avatar Nov 28 '23 09:11 bep

@bep I worked more on this issue and noticed something interesting. In my code where I use afero, I am removing the root folder of the fs instance and this causes the infinite recursion when using the Walk method. Should removing the root be forbidden on all Fs types ? I see in MemMapFs there is a init sync.Once attribute which is used to initialize a "root path" only once. But what if we delete that root path ? It kinda breaks the Fs.

Btw, using Windows here, if ever it matters.

The below test function will reproduce the issue.

func TestWalkRemoveRoot(t *testing.T) {
	defer removeAllTestFiles(t)
	fs := Fss[0]
	rootPath := "."

	err := fs.RemoveAll(rootPath)
	if err != nil {
		t.Error(err)
	}

	testRegistry[fs] = append(testRegistry[fs], rootPath)
	setupTestFiles(t, fs, rootPath)

	walkFn := func(path string, info os.FileInfo, err error) error {
		fmt.Println(path, info.Name(), info.IsDir(), info.Size(), err)
		return err
	}

	err = Walk(fs, rootPath, walkFn)
	if err != nil {
		t.Error(err)
	}
}

Seeing this case implies my proposed fix is just a workaround. The real fix would be to better protect the root path from deletion.

erstam avatar Dec 04 '23 00:12 erstam

@bep, @spf13 any thoughts on my previous comment ?

erstam avatar Dec 12 '23 14:12 erstam

Will we see this merged at some point ?

erstam avatar Feb 07 '24 15:02 erstam