-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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
path/filepath: Clean definition is not idempotent #22861
Comments
Could you please provide a small program that demonstrates the issue.
Thank you
… On 24 Nov 2017, at 09:55, larhun ***@***.***> wrote:
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (go version)?
version 1.9
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (go env)?
windows/amd64
Issue
The algorithm defined for the Clean function is faulty and, in addition, is not faithfully implemented.
In particular, the definition is faulty because the idempotent condition
Clean(Clean(x)) == Clean(x)
could fail, if Separator is not '/', as on Windows systems.
In fact, by the algorithm documentation, on Windows systems '/' would not be processed as Separator, until the final translation to Separator. Thus, applying Clean once or twice (with '/' translated to Separator at the end of the first cleaning step) could return different results, contrary to the idempotent condition.
In addition, the algorithm implementation slightly deviates from the documentation. In fact, every character that satisfies
os.IsPathSeparator(c) == true
is processed as Separator, not only Separator, as stated in the algorithm definition. On Windows systems, this is true for both: '\\' and '/'.
Please note that, if os.IsPathSeparator('/') is true for all available implementations, '/' is always processed as Separator. Thus, idempotence can be magically preserved, despite the errors in the algorithm definition and implementation.
Proposal
I propose to clarify the algorithm documentation and adapt the implementation as follows.
Documentation
change the first sentence to
Clean returns the shortest path name equivalent to path by purely lexical processing. First, it replaces any occurrence of slash by Separator. Then, it applies the following rules iteratively until no further processing can be done:
replace slash with Separator in the sentence
The returned path ends in a slash ...
remove the sentence
Finally, any occurrences of slash are replaced by Separator.
Implementation
add the follwing statement at the first line
path = FromSlash(path)
change any expression like os.IsPathSeparator(x) to
x == Separator
change the last line to:
return out.string()
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
The issue can be demonstrated by a program only if Therefore, to prove that the algorithm definition is faulty, a modified
that is true only for Then, the following program prints
For an idempotent function, the expected value is |
Do I understand correctly that you're saying the issue occurs only when a condition is met - but that condition cannot happen? |
No, I am not saying that. The issue is about documentation and implementation, that are faulty and not consistent. The documentation states:
I am saying that the previous algorithm is not right, because it states that only However, the implementation deviates from the documentation and processes as The documentation should not be misleading. The right algorithm should start by first replacing the slash with |
If the implementation is faulty as claimed, there must be a way how to demonstrate the issue with that faulty implementation. But earlier you wrote:
Is there any other system where |
To clarify my previous notes, there are two errors that translate into a correct behavior.
Therefore,
I propose to change both. |
Any changes to this package will affect millions of users that import it. The correct behavior is the one that causes the least surprise and adheres to the compatibility guidelines. The risk involved in changing the algorithm is that the new algorithm will be incorrect (and that this incorrectness will also be inconsistent). File path handling is subtle, and requires many special cases to get right. That is probably the reason the code looks the way it does now. On a system where '/' isn't a separator, what hope is there for correctness anyway? |
I don't see any reason to change the implementation. If you think that we should change the documentation, by all means send a pull request. Thanks. |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?version 1.9
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?windows/amd64
Issue
The algorithm defined for the
Clean
function is faulty and, in addition, is not faithfully implemented.In particular, the definition is faulty because the idempotent condition
could fail, if
Separator
is not'/'
, as on Windows systems.In fact, by the algorithm documentation, on Windows systems
'/'
would not be processed asSeparator
, until the final translation toSeparator
. Thus, applyingClean
once or twice (with'/'
translated toSeparator
at the end of the first cleaning step) could return different results, contrary to the idempotent condition.In addition, the algorithm implementation slightly deviates from the documentation. In fact, every character that satisfies
is processed as
Separator
, not onlySeparator
, as stated in the algorithm definition. On Windows systems, this is true for both:'\\'
and'/'
.Please note that, if
os.IsPathSeparator('/')
is true for all available implementations,'/'
is always processed asSeparator
. Thus, idempotence can be magically preserved, despite the errors in the algorithm definition and implementation.Proposal
I propose to clarify the algorithm documentation and adapt the implementation as follows.
Documentation
slash
withSeparator
in the sentenceImplementation
add the follwing statement at the first line
path = FromSlash(path)
change any expression like
os.IsPathSeparator(x)
tox == Separator
change the last line to:
return out.string()
The text was updated successfully, but these errors were encountered: