Skip to content

Incorrect way to handle directory read/write access checks in the test command #1116

Closed

Description

@ic-it

Problem Description:
The current implementation of the TsWrite/TsRead case in the code does not correctly determine if a directory is writable/readable.
Specifically, the following snippet:

sh/interp/test.go

Lines 158 to 163 in 86363cf

case syntax.TsWrite:
f, err := r.open(ctx, x, os.O_WRONLY, 0, false)
if err == nil {
f.Close()
}
return err == nil

will fail when x refers to a directory because opening a directory with the os.O_WRONLY flag is not a valid operation. Consequently, the function will always return false for directories, even if they are writable.

Activity

  1. changed the title [-]Incorrect way to handle directory Read/Write access check[/-] [+]Incorrect way to handle directory Read/Write access check in `test` command[/+] on Dec 30, 2024
  2. changed the title [-]Incorrect way to handle directory Read/Write access check in `test` command[/-] [+]Incorrect way to handle directory read/write access checks in the test command[/+] on Dec 30, 2024
  3. ic-it commented on Jan 2, 2025

    @ic-it
    Author

    In this case, I suggest, use these solutions from here

    Since there is no single and cross-platform solution, we can do it differently for different systems via +build

    There is also an option to create a file to "check" if it can be written to the directory. But this approach is IMHO error-prone

  4. mvdan commented on Jan 2, 2025

    @mvdan
    Owner

    We already have some OS-dependent logic via os_unix.go and os_notunix.go. It would be perfectly fine to extend this to perform better on unix as you suggest; we would just need tests to cover the edge case above.

  5. added a commit that references this issue on Feb 22, 2025
    ba62f13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions