From 488472d754a70d6b1c98e4d901dff438257ab06d Mon Sep 17 00:00:00 2001 From: Jethro Beekman Date: Fri, 13 Jul 2018 10:14:16 -0700 Subject: [PATCH] Don't silently ignore invalid data in target spec --- src/librustc_target/spec/mod.rs | 26 +++++++++++-------- .../my-x86_64-unknown-linux-gnu-platform.json | 2 +- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/librustc_target/spec/mod.rs b/src/librustc_target/spec/mod.rs index 19e353c6a942d..07c3ae6973969 100644 --- a/src/librustc_target/spec/mod.rs +++ b/src/librustc_target/spec/mod.rs @@ -861,23 +861,27 @@ impl Target { } ); ($key_name:ident, link_args) => ( { let name = (stringify!($key_name)).replace("_", "-"); - if let Some(obj) = obj.find(&name[..]).and_then(|o| o.as_object()) { + if let Some(val) = obj.find(&name[..]) { + let obj = val.as_object().ok_or_else(|| format!("{}: expected a \ + JSON object with fields per linker-flavor.", name))?; let mut args = LinkArgs::new(); for (k, v) in obj { - let k = LinkerFlavor::from_str(&k).ok_or_else(|| { + let flavor = LinkerFlavor::from_str(&k).ok_or_else(|| { format!("{}: '{}' is not a valid value for linker-flavor. \ Use 'em', 'gcc', 'ld' or 'msvc'", name, k) })?; - let v = v.as_array().map(|a| { - a - .iter() - .filter_map(|o| o.as_string()) - .map(|s| s.to_owned()) - .collect::>() - }).unwrap_or(vec![]); - - args.insert(k, v); + let v = v.as_array().ok_or_else(|| + format!("{}.{}: expected a JSON array", name, k) + )?.iter().enumerate() + .map(|(i,s)| { + let s = s.as_string().ok_or_else(|| + format!("{}.{}[{}]: expected a JSON string", name, k, i))?; + Ok(s.to_owned()) + }) + .collect::, String>>()?; + + args.insert(flavor, v); } base.options.$key_name = args; } diff --git a/src/test/run-make-fulldeps/target-specs/my-x86_64-unknown-linux-gnu-platform.json b/src/test/run-make-fulldeps/target-specs/my-x86_64-unknown-linux-gnu-platform.json index 3ae01d72fcc18..48040ae3da0ef 100644 --- a/src/test/run-make-fulldeps/target-specs/my-x86_64-unknown-linux-gnu-platform.json +++ b/src/test/run-make-fulldeps/target-specs/my-x86_64-unknown-linux-gnu-platform.json @@ -1,5 +1,5 @@ { - "pre-link-args": ["-m64"], + "pre-link-args": {"gcc": ["-m64"]}, "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128", "linker-flavor": "gcc", "llvm-target": "x86_64-unknown-linux-gnu",