Skip to content

Commit 9e3bf02

Browse files
committed
auto merge of #17472 : kaseyc/rust/ICE_fix, r=aturon
Add checks for null bytes in the value strings for the export_name and link_section attributes, reporting an error if any are found, before calling with_c_str on them. Fixes #16478
2 parents 5366cfe + 3e8ad53 commit 9e3bf02

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/librustc/middle/trans/base.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,6 +2670,10 @@ fn exported_name(ccx: &CrateContext, id: ast::NodeId,
26702670
}
26712671
}
26722672

2673+
fn contains_null(s: &str) -> bool {
2674+
s.bytes().any(|b| b == 0)
2675+
}
2676+
26732677
pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {
26742678
debug!("get_item_val(id=`{:?}`)", id);
26752679

@@ -2701,6 +2705,11 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {
27012705

27022706
unsafe {
27032707
let llty = llvm::LLVMTypeOf(v);
2708+
if contains_null(sym.as_slice()) {
2709+
ccx.sess().fatal(
2710+
format!("Illegal null byte in export_name value: `{}`",
2711+
sym).as_slice());
2712+
}
27042713
let g = sym.as_slice().with_c_str(|buf| {
27052714
llvm::LLVMAddGlobal(ccx.llmod(), llty, buf)
27062715
});
@@ -2764,10 +2773,16 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {
27642773

27652774
match attr::first_attr_value_str_by_name(i.attrs.as_slice(),
27662775
"link_section") {
2767-
Some(sect) => unsafe {
2768-
sect.get().with_c_str(|buf| {
2769-
llvm::LLVMSetSection(v, buf);
2770-
})
2776+
Some(sect) => {
2777+
if contains_null(sect.get()) {
2778+
ccx.sess().fatal(format!("Illegal null byte in link_section value: `{}`",
2779+
sect.get()).as_slice());
2780+
}
2781+
unsafe {
2782+
sect.get().with_c_str(|buf| {
2783+
llvm::LLVMSetSection(v, buf);
2784+
})
2785+
}
27712786
},
27722787
None => ()
27732788
}

0 commit comments

Comments
 (0)