Skip to content

Commit

Permalink
Check for null bytes before calling with_c_str on link_section and ex…
Browse files Browse the repository at this point in the history
…port_name value strings.
  • Loading branch information
kaseyc committed Sep 23, 2014
1 parent 3941d3c commit 80014b2
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/librustc/middle/trans/base.rs
Expand Up @@ -2670,6 +2670,15 @@ fn exported_name(ccx: &CrateContext, id: ast::NodeId,
}
}

fn contains_null(s: &str) -> bool {
for b in s.bytes() {
if b == 0 {
return true
}
}
false
}

pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {
debug!("get_item_val(id=`{:?}`)", id);

Expand Down Expand Up @@ -2701,6 +2710,11 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {

unsafe {
let llty = llvm::LLVMTypeOf(v);
if contains_null(sym.as_slice()) {
ccx.sess().fatal(
format!("Illegal null byte in export_name value: `{}`",
sym).as_slice());
}
let g = sym.as_slice().with_c_str(|buf| {
llvm::LLVMAddGlobal(ccx.llmod(), llty, buf)
});
Expand Down Expand Up @@ -2764,10 +2778,16 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {

match attr::first_attr_value_str_by_name(i.attrs.as_slice(),
"link_section") {
Some(sect) => unsafe {
sect.get().with_c_str(|buf| {
llvm::LLVMSetSection(v, buf);
})
Some(sect) => {
if contains_null(sect.get()) {
ccx.sess().fatal(format!("Illegal null byte in link_section value: `{}`",
sect.get()).as_slice());
}
unsafe {
sect.get().with_c_str(|buf| {
llvm::LLVMSetSection(v, buf);
})
}
},
None => ()
}
Expand Down

0 comments on commit 80014b2

Please sign in to comment.