-
Notifications
You must be signed in to change notification settings - Fork 18k
x/tools/godoc/vfs/mapfs: easy to accidentally misuse API, causing severe problems #34591
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
Comments
Change https://golang.org/cl/197859 mentions this issue: |
I think this can be fixed in the following ways:
I've sent a CL that implements option 1 to see if it'd catch any invalid usages in x/tools, and there weren't any. The downside of option 3 is that it makes API harder to use: when leading slash is optional, it's no longer clear whether one should include it or not. It will lead to more inconsistent styles. It makes That leaves 1 or 2. Given there are no invalid usages in x/tools (nor in x/website, I checked), I'd prefer 1. I believe a hard-to-miss panic will save people time and cause fewer problems in the long term. |
I've thought about this quite a bit, and I still think option 1 has the best trade-offs based on what we know now. If this newly added panic happens somewhere, I currently believe learning about that will bring greater positive value than the disruption it would cause. If we learn of new evidence suggesting otherwise, we can revert and consider another approach. |
mapfs.New documentation says: > Map keys should be forward slash-separated pathnames > and not contain a leading slash. It is invalid input if a provided path contains a leading slash, and it causes the returned filesystem to have undefined behavior. Package mapfs is often used in tests, so this can lead to a serious problem elsewhere. Help detect invalid API usage sooner by validating input, and panicking when it contains leading slashes. Programs that use mapfs API correctly will be unaffected, and it will be faster for incorrect programs—if any exist today or will be created in the future—to detect and correct such problems. Fixes golang/go#34591. Change-Id: I77e5f0f4628edf83480604135f58bfb62e521d80 Reviewed-on: https://go-review.googlesource.com/c/tools/+/197859 Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
mapfs.New
documentation says:However, there is no validation on the input. If the user accidentally provides map keys with leading slashes, it results in incorrect programs and hard-to-diagnose problems.
For example:
It produces output:
When
mapfs.New
is used correctly, like so:Then the output is as expected:
I think it would be net helpful to perform validation in
New
and help users learn as quickly as possible when they're misusing the API by panicking with a descriptive error message. Panics aren't good, but incorrect results can be even worse. Mostmapfs.New
usage happens at program initialization time and in tests./cc @jayconrod
The text was updated successfully, but these errors were encountered: