From ca2ded2b40eeb69ba7dda3facb2b7742f13b36a8 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sat, 16 Jul 2022 23:32:22 +0700 Subject: lib/memfs: set the Root SysPath to the first MemFS instance on Merge Previously, calling Merge(...), set the merged MemFS Root.SysPath to "..". Since we allow the TryDirect to access the file directly (if its set to true), this may cause the file system leaks if returned MemFS set this flag to true. To prevent that, we set the SysPath to the first MemFS SysPath. --- lib/memfs/memfs.go | 14 +++++++++++++- lib/memfs/memfs_test.go | 4 ++-- 2 files changed, 15 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/memfs/memfs.go b/lib/memfs/memfs.go index c55311bd..c1fccea9 100644 --- a/lib/memfs/memfs.go +++ b/lib/memfs/memfs.go @@ -42,6 +42,9 @@ type MemFS struct { // Merge one or more instances of MemFS into single hierarchy. // +// The returned MemFS instance will have SysPath set to the first +// MemFS.SysPath in parameter. +// // If there are two instance of Node that have the same path, the last // instance will be ignored. func Merge(params ...*MemFS) (merged *MemFS) { @@ -57,7 +60,16 @@ func Merge(params ...*MemFS) (merged *MemFS) { merged.PathNodes.Set("/", merged.Root) - for _, mfs := range params { + var ( + x int + mfs *MemFS + ) + + for x, mfs = range params { + if x == 0 { + merged.Root.SysPath = mfs.Root.SysPath + } + for _, child := range mfs.Root.Childs { gotNode := merged.PathNodes.Get(child.Path) if gotNode != nil { diff --git a/lib/memfs/memfs_test.go b/lib/memfs/memfs_test.go index 22c71bf5..f61d2930 100644 --- a/lib/memfs/memfs_test.go +++ b/lib/memfs/memfs_test.go @@ -576,7 +576,7 @@ func TestMerge(t *testing.T) { PathNodes: &PathNode{ v: map[string]*Node{ "/": &Node{ - SysPath: "..", + SysPath: mfsDirect.Root.SysPath, Path: "/", Childs: []*Node{ mfsDirect.MustGet("/add"), @@ -596,7 +596,7 @@ func TestMerge(t *testing.T) { PathNodes: &PathNode{ v: map[string]*Node{ "/": &Node{ - SysPath: "..", + SysPath: mfsDirect.Root.SysPath, Path: "/", Childs: []*Node{ mfsDirect.MustGet("/add"), -- cgit v1.3