]> git.lizzy.rs Git - rust.git/commitdiff
Avoid panicking when invalid argument is passed to cfg(..)
authorSeiichi Uchida <seuchida@gmail.com>
Fri, 12 Jan 2018 02:41:33 +0000 (11:41 +0900)
committerSeiichi Uchida <seuchida@gmail.com>
Fri, 12 Jan 2018 12:48:17 +0000 (21:48 +0900)
Closes #43925.
Closes #43926.

src/librustc_metadata/native_libs.rs
src/test/compile-fail/issue-43925.rs [new file with mode: 0644]
src/test/compile-fail/issue-43926.rs [new file with mode: 0644]

index cc332acb5b08e8315aff614941e8523fc7ee447b..c0ce32cc970683effe0ae338c303e681dcb40a98 100644 (file)
@@ -92,9 +92,19 @@ fn visit_item(&mut self, it: &'tcx hir::Item) {
             let cfg = items.iter().find(|k| {
                 k.check_name("cfg")
             }).and_then(|a| a.meta_item_list());
-            let cfg = cfg.map(|list| {
-                list[0].meta_item().unwrap().clone()
-            });
+            let cfg = if let Some(list) = cfg {
+                if list.is_empty() {
+                    self.tcx.sess.span_err(m.span(), "`cfg()` must have an argument");
+                    return;
+                } else if let cfg @ Some(..) = list[0].meta_item() {
+                    cfg.cloned()
+                } else {
+                    self.tcx.sess.span_err(list[0].span(), "invalid argument for `cfg(..)`");
+                    return;
+                }
+            } else {
+                None
+            };
             let foreign_items = fm.items.iter()
                 .map(|it| self.tcx.hir.local_def_id(it.id))
                 .collect();
diff --git a/src/test/compile-fail/issue-43925.rs b/src/test/compile-fail/issue-43925.rs
new file mode 100644 (file)
index 0000000..8ad5764
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(attr_literals)]
+
+#[link(name="foo", cfg("rlib"))] //~ ERROR invalid argument for `cfg(..)`
+extern {}
+
+fn main() {}
diff --git a/src/test/compile-fail/issue-43926.rs b/src/test/compile-fail/issue-43926.rs
new file mode 100644 (file)
index 0000000..5d510b6
--- /dev/null
@@ -0,0 +1,14 @@
+// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#[link(name="foo", cfg())] //~ ERROR `cfg()` must have an argument
+extern {}
+
+fn main() {}