Avoid aliasing in fsnode by forcing copies for file contents and add a test

This commit is contained in:
Francesc Campoy
2021-07-16 12:49:57 -07:00
parent 35d1c3f9b4
commit 803885049b
2 changed files with 7 additions and 3 deletions

View File

@@ -123,11 +123,11 @@ func (n *fsNode) addFile(name string, c []byte) (result *fsNode, err error) {
if result.offset != nil {
return nil, fmt.Errorf("cannot add already opened file '%s'", n.Path())
}
result.content = c
result.content = append(result.content[:0], c...)
return result, nil
}
result = &fsNode{
content: c,
content: append([]byte(nil), c...),
parent: parent,
}
parent.dir[fileName] = result

View File

@@ -157,9 +157,13 @@ func runBasicOperations(
if string(stuff) != both {
t.Fatalf("%s; unexpected content '%s', expected '%s'", c.what, stuff, both)
}
if err := fSys.WriteFile(c.path, []byte(shortContent)); err != nil {
content := []byte(shortContent)
if err := fSys.WriteFile(c.path, content); err != nil {
t.Fatalf("%s; unexpected error: %v", c.what, err)
}
// This ensures that modifying the original slice does not change the contents of the file.
content[0] = '@'
stuff, err = fSys.ReadFile(c.path)
if err != nil {
t.Fatalf("%s; unexpected error: %v", c.what, err)