Skip to content

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

@earthboundkid

Description

@earthboundkid

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions