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

Refactor and fix parse_prefix on Windows #78833

Merged
merged 1 commit into from
Dec 16, 2020
Merged

Conversation

CDirkx
Copy link
Contributor

@CDirkx CDirkx commented Nov 7, 2020

This PR is an extension of #78692 as well as a general refactor of parse_prefix:

Fixes:
There are two errors in the current implementation of parse_prefix:

Firstly, in the current implementation only \ is recognized as a separator character in device namespace prefixes. This behavior is only correct for verbatim paths; "\\.\C:/foo" should be parsed as "C:" instead of "C:/foo".

Secondly, the current implementation only handles single separator characters. In non-verbatim paths a series of separator characters should be recognized as a single boundary, e.g. the UNC path "\\localhost\\\\\\C$\foo" should be parsed as "\\localhost\\\\\\C$" and then UNC(server: "localhost", share: "C$"), but currently it is not parsed at all, because it starts being parsed as \\localhost\ and then has an invalid empty share location.

Paths like "\\.\C:/foo" and "\\localhost\\\\\\C$\foo" are valid on Windows, they are equivalent to just "C:\foo".

Refactoring:
All uses of &[u8] within parse_prefix are extracted to helper functions and&OsStr is used instead. This reduces the number of places unsafe is used:

  • get_first_two_components is adapted to the more general parse_next_component and used in more places
  • code for parsing drive prefixes is extracted to parse_drive

@rust-highfive
Copy link
Collaborator

r? @LukasKalbertodt

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 7, 2020
Refactor `get_first_two_components` to `get_next_component`.

Fixes the following behaviour of `parse_prefix`:
 - series of separator bytes in a prefix are correctly parsed as a single separator
 - device namespace prefixes correctly recognize both `\\` and `/` as separators
@Dylan-DPC-zz
Copy link

r? @dtolnay

@dtolnay
Copy link
Member

dtolnay commented Dec 6, 2020

@rustbot ping windows

This PR changes the behavior of Path::new(...).iter() for some Windows paths. Would it be possible to get a sanity check that the description above matches what you would expect?

@rustbot
Copy link
Collaborator

rustbot commented Dec 6, 2020

Hey Windows Group! This bug has been identified as a good "Windows candidate".
In case it's useful, here are some instructions for tackling these sorts of
bugs. Maybe take a look?
Thanks! <3

cc @arlosi @danielframpton @gdr-at-ms @kennykerr @luqmana @lzybkr @retep998 @rylev @sivadeilra

@rustbot rustbot added the O-windows Operating system: Windows label Dec 6, 2020
@sivadeilra
Copy link

Sure, I'll take a look on Monday.

@sivadeilra
Copy link

Windows dev, here. On principle, this change seems sound. There's always the risk of breaking some obscure corner case when it comes to parsing paths on Windows, but I don't think that should stop this PR.

Agreed w/ @dtolnay on getting some form of sanity check for the behavior of Path::iter().

@dtolnay
Copy link
Member

dtolnay commented Dec 13, 2020

By sanity check I just meant someone else's opinion familiar with Windows paths. The implementation and test updates in the PR look good to me.

Thanks @CDirkx and @sivadeilra!

@dtolnay
Copy link
Member

dtolnay commented Dec 13, 2020

@bors r+

@bors
Copy link
Contributor

bors commented Dec 13, 2020

📌 Commit 94d73d4 has been approved by dtolnay

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 13, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Dec 14, 2020
Refactor and fix `parse_prefix` on Windows

This PR is an extension of rust-lang#78692 as well as a general refactor of `parse_prefix`:

**Fixes**:
There are two errors in the current implementation of `parse_prefix`:

Firstly, in the current implementation only `\` is recognized as a separator character in device namespace prefixes. This behavior is only correct for verbatim paths; `"\\.\C:/foo"` should be parsed as `"C:"` instead of `"C:/foo"`.

Secondly, the current implementation only handles single separator characters. In non-verbatim paths a series of separator characters should be recognized as a single boundary, e.g. the UNC path `"\\localhost\\\\\\C$\foo"` should be parsed as `"\\localhost\\\\\\C$"` and then `UNC(server: "localhost", share: "C$")`, but currently it is not parsed at all, because it starts being parsed as `\\localhost\` and then has an invalid empty share location.

Paths like `"\\.\C:/foo"` and `"\\localhost\\\\\\C$\foo"` are valid on Windows, they are equivalent to just `"C:\foo"`.

**Refactoring**:
All uses of `&[u8]` within `parse_prefix` are extracted to helper functions and`&OsStr` is used instead. This reduces the number of places unsafe is used:
- `get_first_two_components` is adapted to the more general `parse_next_component` and used in more places
- code for parsing drive prefixes is extracted to `parse_drive`
@bors
Copy link
Contributor

bors commented Dec 14, 2020

⌛ Testing commit 94d73d4 with merge 906a5b35869e78b67c0fc3890065bb570bd0b4f1...

@bors
Copy link
Contributor

bors commented Dec 14, 2020

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Dec 14, 2020
@dtolnay
Copy link
Member

dtolnay commented Dec 15, 2020

@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 15, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Dec 15, 2020
Refactor and fix `parse_prefix` on Windows

This PR is an extension of rust-lang#78692 as well as a general refactor of `parse_prefix`:

**Fixes**:
There are two errors in the current implementation of `parse_prefix`:

Firstly, in the current implementation only `\` is recognized as a separator character in device namespace prefixes. This behavior is only correct for verbatim paths; `"\\.\C:/foo"` should be parsed as `"C:"` instead of `"C:/foo"`.

Secondly, the current implementation only handles single separator characters. In non-verbatim paths a series of separator characters should be recognized as a single boundary, e.g. the UNC path `"\\localhost\\\\\\C$\foo"` should be parsed as `"\\localhost\\\\\\C$"` and then `UNC(server: "localhost", share: "C$")`, but currently it is not parsed at all, because it starts being parsed as `\\localhost\` and then has an invalid empty share location.

Paths like `"\\.\C:/foo"` and `"\\localhost\\\\\\C$\foo"` are valid on Windows, they are equivalent to just `"C:\foo"`.

**Refactoring**:
All uses of `&[u8]` within `parse_prefix` are extracted to helper functions and`&OsStr` is used instead. This reduces the number of places unsafe is used:
- `get_first_two_components` is adapted to the more general `parse_next_component` and used in more places
- code for parsing drive prefixes is extracted to `parse_drive`
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Dec 15, 2020
Refactor and fix `parse_prefix` on Windows

This PR is an extension of rust-lang#78692 as well as a general refactor of `parse_prefix`:

**Fixes**:
There are two errors in the current implementation of `parse_prefix`:

Firstly, in the current implementation only `\` is recognized as a separator character in device namespace prefixes. This behavior is only correct for verbatim paths; `"\\.\C:/foo"` should be parsed as `"C:"` instead of `"C:/foo"`.

Secondly, the current implementation only handles single separator characters. In non-verbatim paths a series of separator characters should be recognized as a single boundary, e.g. the UNC path `"\\localhost\\\\\\C$\foo"` should be parsed as `"\\localhost\\\\\\C$"` and then `UNC(server: "localhost", share: "C$")`, but currently it is not parsed at all, because it starts being parsed as `\\localhost\` and then has an invalid empty share location.

Paths like `"\\.\C:/foo"` and `"\\localhost\\\\\\C$\foo"` are valid on Windows, they are equivalent to just `"C:\foo"`.

**Refactoring**:
All uses of `&[u8]` within `parse_prefix` are extracted to helper functions and`&OsStr` is used instead. This reduces the number of places unsafe is used:
- `get_first_two_components` is adapted to the more general `parse_next_component` and used in more places
- code for parsing drive prefixes is extracted to `parse_drive`
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Dec 15, 2020
Refactor and fix `parse_prefix` on Windows

This PR is an extension of rust-lang#78692 as well as a general refactor of `parse_prefix`:

**Fixes**:
There are two errors in the current implementation of `parse_prefix`:

Firstly, in the current implementation only `\` is recognized as a separator character in device namespace prefixes. This behavior is only correct for verbatim paths; `"\\.\C:/foo"` should be parsed as `"C:"` instead of `"C:/foo"`.

Secondly, the current implementation only handles single separator characters. In non-verbatim paths a series of separator characters should be recognized as a single boundary, e.g. the UNC path `"\\localhost\\\\\\C$\foo"` should be parsed as `"\\localhost\\\\\\C$"` and then `UNC(server: "localhost", share: "C$")`, but currently it is not parsed at all, because it starts being parsed as `\\localhost\` and then has an invalid empty share location.

Paths like `"\\.\C:/foo"` and `"\\localhost\\\\\\C$\foo"` are valid on Windows, they are equivalent to just `"C:\foo"`.

**Refactoring**:
All uses of `&[u8]` within `parse_prefix` are extracted to helper functions and`&OsStr` is used instead. This reduces the number of places unsafe is used:
- `get_first_two_components` is adapted to the more general `parse_next_component` and used in more places
- code for parsing drive prefixes is extracted to `parse_drive`
@bors
Copy link
Contributor

bors commented Dec 16, 2020

⌛ Testing commit 94d73d4 with merge c00a464...

@bors
Copy link
Contributor

bors commented Dec 16, 2020

☀️ Test successful - checks-actions
Approved by: dtolnay
Pushing c00a464 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 16, 2020
@bors bors merged commit c00a464 into rust-lang:master Dec 16, 2020
@rustbot rustbot added this to the 1.50.0 milestone Dec 16, 2020
@CDirkx CDirkx deleted the parse_prefix branch January 9, 2021 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. O-windows Operating system: Windows S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants