]> Cypherpunks repositories - gostls13.git/commitdiff
io/fs: document that caller can modify slice returned by ReadFile
authorIan Lance Taylor <iant@golang.org>
Mon, 19 Apr 2021 23:05:01 +0000 (16:05 -0700)
committerIan Lance Taylor <iant@golang.org>
Wed, 21 Apr 2021 16:46:52 +0000 (16:46 +0000)
Also add a test to testing/fstest.

Fixes #45186

Change-Id: I00e5f46ccd5269dbc266a8f2ebc9a62ebb1297b8
Reviewed-on: https://go-review.googlesource.com/c/go/+/311649
Trust: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/io/fs/readfile.go
src/testing/fstest/testfs.go

index 7ee9eadac4d92226c6cd203a6d5491c0ccd4ccb4..d3c181c0a9e60461acdeac9877e81d73f8d0ab83 100644 (file)
@@ -15,6 +15,9 @@ type ReadFileFS interface {
        // A successful call returns a nil error, not io.EOF.
        // (Because ReadFile reads the whole file, the expected EOF
        // from the final Read is not treated as an error to be reported.)
+       //
+       // The caller is permitted to modify the returned byte slice.
+       // This method should return a copy of the underlying data.
        ReadFile(name string) ([]byte, error)
 }
 
index 80ca0e9a1d7fdeada1ae6776b07a585bcad553ba..5c4f30af168422c75c05b2c272b9fbc430320592 100644 (file)
@@ -537,6 +537,18 @@ func (t *fsTester) checkFile(file string) {
                }
                t.checkFileRead(file, "ReadAll vs fsys.ReadFile", data, data2)
 
+               // Modify the data and check it again. Modifying the
+               // returned byte slice should not affect the next call.
+               for i := range data2 {
+                       data2[i]++
+               }
+               data2, err = fsys.ReadFile(file)
+               if err != nil {
+                       t.errorf("%s: second call to fsys.ReadFile: %v", file, err)
+                       return
+               }
+               t.checkFileRead(file, "Readall vs second fsys.ReadFile", data, data2)
+
                t.checkBadPath(file, "ReadFile",
                        func(name string) error { _, err := fsys.ReadFile(name); return err })
        }