Skip to content

Commit 520ec9b

Browse files
petrochenkovpietroalbini
authored andcommitted
Validate syntax of --cfg command line arguments
1 parent fbc120c commit 520ec9b

File tree

8 files changed

+40
-40
lines changed

8 files changed

+40
-40
lines changed

src/librustc/session/config.rs

+25-14
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_data_structures::stable_hasher::ToStableHashKey;
2828
use lint;
2929
use middle::cstore;
3030

31-
use syntax::ast::{self, IntTy, UintTy};
31+
use syntax::ast::{self, IntTy, UintTy, MetaItemKind};
3232
use syntax::codemap::{FileName, FilePathMapping};
3333
use syntax::edition::{Edition, EDITION_NAME_LIST, DEFAULT_EDITION};
3434
use syntax::parse::token;
@@ -1746,22 +1746,33 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> ast::CrateConfig {
17461746
let mut parser =
17471747
parse::new_parser_from_source_str(&sess, FileName::CfgSpec, s.to_string());
17481748

1749-
let meta_item = panictry!(parser.parse_meta_item());
1749+
macro_rules! error {($reason: expr) => {
1750+
early_error(ErrorOutputType::default(),
1751+
&format!(concat!("invalid `--cfg` argument: `{}` (", $reason, ")"), s));
1752+
}}
17501753

1751-
if parser.token != token::Eof {
1752-
early_error(
1753-
ErrorOutputType::default(),
1754-
&format!("invalid --cfg argument: {}", s),
1755-
)
1756-
} else if meta_item.is_meta_item_list() {
1757-
let msg = format!(
1758-
"invalid predicate in --cfg command line argument: `{}`",
1759-
meta_item.ident
1760-
);
1761-
early_error(ErrorOutputType::default(), &msg)
1754+
match &mut parser.parse_meta_item() {
1755+
Ok(meta_item) if parser.token == token::Eof => {
1756+
if meta_item.ident.segments.len() != 1 {
1757+
error!("argument key must be an identifier");
1758+
}
1759+
match &meta_item.node {
1760+
MetaItemKind::List(..) => {
1761+
error!(r#"expected `key` or `key="value"`"#);
1762+
}
1763+
MetaItemKind::NameValue(lit) if !lit.node.is_str() => {
1764+
error!("argument value must be a string");
1765+
}
1766+
MetaItemKind::NameValue(..) | MetaItemKind::Word => {
1767+
return (meta_item.name(), meta_item.value_str());
1768+
}
1769+
}
1770+
}
1771+
Ok(..) => {}
1772+
Err(err) => err.cancel(),
17621773
}
17631774

1764-
(meta_item.name(), meta_item.value_str())
1775+
error!(r#"expected `key` or `key="value"`"#);
17651776
})
17661777
.collect::<ast::CrateConfig>()
17671778
}

src/test/compile-fail/cfg-arg-invalid.rs

-13
This file was deleted.

src/test/ui/cfg-arg-invalid-1.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// compile-flags: --cfg a(b=c)
2+
// error-pattern: invalid `--cfg` argument: `a(b=c)` (expected `key` or `key="value"`)
3+
fn main() {}

src/test/ui/cfg-arg-invalid-2.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// compile-flags: --cfg a{b}
2+
// error-pattern: invalid `--cfg` argument: `a{b}` (expected `key` or `key="value"`)
3+
fn main() {}

src/test/ui/cfg-arg-invalid-3.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// compile-flags: --cfg a::b
2+
// error-pattern: invalid `--cfg` argument: `a::b` (argument key must be an identifier)
3+
fn main() {}

src/test/ui/cfg-arg-invalid-4.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// compile-flags: --cfg a(b)
2+
// error-pattern: invalid `--cfg` argument: `a(b)` (expected `key` or `key="value"`)
3+
fn main() {}

src/test/ui/cfg-arg-invalid-5.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// compile-flags: --cfg a=10
2+
// error-pattern: invalid `--cfg` argument: `a=10` (argument value must be a string)
3+
fn main() {}

src/test/ui/issue-31495.rs

-13
This file was deleted.

0 commit comments

Comments
 (0)