Remove inappropriate .to_c_str() in C_cstr()

LLVMConstStringInContext() doesn't need a null-terminated string. It
takes a length instead. Using .to_c_str() here triggers an ICE whenever
the string literal embeds a null, as in "\x00".
This commit is contained in:
Kevin Ballard 2013-08-14 23:40:28 -07:00
parent f744cf157e
commit 5ca4cdc7b8
2 changed files with 4 additions and 4 deletions

View File

@ -36,7 +36,7 @@ use std::c_str::ToCStr;
use std::cast::transmute; use std::cast::transmute;
use std::cast; use std::cast;
use std::hashmap::{HashMap}; use std::hashmap::{HashMap};
use std::libc::{c_uint, c_longlong, c_ulonglong}; use std::libc::{c_uint, c_longlong, c_ulonglong, c_char};
use std::vec; use std::vec;
use syntax::ast::ident; use syntax::ast::ident;
use syntax::ast_map::{path, path_elt}; use syntax::ast_map::{path, path_elt};
@ -760,8 +760,8 @@ pub fn C_cstr(cx: &mut CrateContext, s: @str) -> ValueRef {
None => () None => ()
} }
let sc = do s.with_c_str |buf| { let sc = do s.as_imm_buf |buf, buflen| {
llvm::LLVMConstStringInContext(cx.llcx, buf, s.len() as c_uint, False) llvm::LLVMConstStringInContext(cx.llcx, buf as *c_char, buflen as c_uint, False)
}; };
let gsym = token::gensym("str"); let gsym = token::gensym("str");

View File

@ -265,7 +265,7 @@ pub fn trans_lit_str(bcx: @mut Block,
Ignore => bcx, Ignore => bcx,
SaveIn(lldest) => { SaveIn(lldest) => {
unsafe { unsafe {
let bytes = str_lit.len(); // count null-terminator too let bytes = str_lit.len();
let llbytes = C_uint(bcx.ccx(), bytes); let llbytes = C_uint(bcx.ccx(), bytes);
let llcstr = C_cstr(bcx.ccx(), str_lit); let llcstr = C_cstr(bcx.ccx(), str_lit);
let llcstr = llvm::LLVMConstPointerCast(llcstr, Type::i8p().to_ref()); let llcstr = llvm::LLVMConstPointerCast(llcstr, Type::i8p().to_ref());