Skip to content

Use relative-path for logical path traversal #4831

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ rustfmt-config_proc_macro = { version = "0.2", path = "config_proc_macro" }
lazy_static = "1.0.0"
anyhow = "1.0"
thiserror = "1.0"
relative-path = "1.4.0"

# A noop dependency that changes in the Rust repository, it's a bit of a hack.
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2021-05-04"
channel = "nightly-2021-05-01"
components = ["rustc-dev"]
24 changes: 15 additions & 9 deletions src/syntux/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::path::{Path, PathBuf};

use relative_path::RelativePath;
use rustc_ast::token::{DelimToken, TokenKind};
use rustc_ast::{ast, ptr};
use rustc_errors::Diagnostic;
Expand Down Expand Up @@ -94,16 +95,21 @@ pub(crate) enum ParserError {
}

impl<'a> Parser<'a> {
// On windows, if the base path is specified with the windows path separator
// replace it with the expected '//' so that path traversal works as
// expected.
#[cfg(windows)]
pub(crate) fn submod_path_from_attr(attrs: &[ast::Attribute], path: &Path) -> Option<PathBuf> {
let path_string = first_attr_value_str_by_name(attrs, sym::path)?.as_str();
// On windows, the base path might have the form
// `\\?\foo\bar` in which case it does not tolerate
// mixed `/` and `\` separators, so canonicalize
// `/` to `\`.
#[cfg(windows)]
let path_string = path_string.replace("/", "\\");

Some(path.join(&*path_string))
let mod_path = first_attr_value_str_by_name(attrs, sym::path)?
.as_str()
.replace(std::path::MAIN_SEPARATOR, "/");
Some(RelativePath::new(&mod_path).to_logical_path(&path))
}

#[cfg(not(windows))]
pub(crate) fn submod_path_from_attr(attrs: &[ast::Attribute], path: &Path) -> Option<PathBuf> {
let mod_path = first_attr_value_str_by_name(attrs, sym::path)?.as_str();
Some(RelativePath::new(&mod_path).to_logical_path(&path))
}

pub(crate) fn parse_file_as_module(
Expand Down