Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io/fs: document whether []byte from ReadFile is (un)safe to modify #45186

Open
carlmjohnson opened this issue Mar 23, 2021 · 1 comment
Open

io/fs: document whether []byte from ReadFile is (un)safe to modify #45186

carlmjohnson opened this issue Mar 23, 2021 · 1 comment

Comments

@carlmjohnson
Copy link
Contributor

@carlmjohnson carlmjohnson commented Mar 23, 2021

Here are the current docs for fs.ReadFileFS's ReadFile method:

    // ReadFile reads the named file and returns its contents.
    // 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.)
    ReadFile(name string) ([]byte, error)

This does not specify whether the returned []byte must be safe to modify or not. I can see the argument for either. It's better for efficiency if it is unsafe and better for safety if it is safe. I think this needs to be documented because Hyrum's Law/XKCD 1172 ensures that very soon some ReadFileFS implementation will be made more efficient by returning an unmodifiable backing store []byte and some clients will be made more efficient by modifying the return value of fs.ReadFile, which will lead to bugs.

@ianlancetaylor ianlancetaylor added this to the Go1.17 milestone Mar 23, 2021
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 23, 2021

CC @rsc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants