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

Closed
earthboundkid opened this issue Mar 23, 2021 · 2 comments
Closed
Labels
Documentation FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@earthboundkid
Copy link
Contributor

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

CC @rsc

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 25, 2021
@gopherbot
Copy link

Change https://golang.org/cl/311649 mentions this issue: io/fs: document that caller can modify slice returned by ReadFile

@golang golang locked and limited conversation to collaborators Apr 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants