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

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

Open
ic-it opened this issue Dec 30, 2024 · 2 comments
Open

Comments

@ic-it
Copy link

ic-it commented Dec 30, 2024

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.

@ic-it ic-it changed the title Incorrect way to handle directory Read/Write access check Incorrect way to handle directory Read/Write access check in test command Dec 30, 2024
@ic-it ic-it 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 Dec 30, 2024
@ic-it
Copy link
Author

ic-it commented Jan 2, 2025

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

@mvdan
Copy link
Owner

mvdan commented Jan 2, 2025

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.

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

No branches or pull requests

2 participants