Skip to content

Commit 5ca4cdc

Browse files
committed
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".
1 parent f744cf1 commit 5ca4cdc

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/librustc/middle/trans/common.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use std::c_str::ToCStr;
3636
use std::cast::transmute;
3737
use std::cast;
3838
use std::hashmap::{HashMap};
39-
use std::libc::{c_uint, c_longlong, c_ulonglong};
39+
use std::libc::{c_uint, c_longlong, c_ulonglong, c_char};
4040
use std::vec;
4141
use syntax::ast::ident;
4242
use syntax::ast_map::{path, path_elt};
@@ -760,8 +760,8 @@ pub fn C_cstr(cx: &mut CrateContext, s: @str) -> ValueRef {
760760
None => ()
761761
}
762762

763-
let sc = do s.with_c_str |buf| {
764-
llvm::LLVMConstStringInContext(cx.llcx, buf, s.len() as c_uint, False)
763+
let sc = do s.as_imm_buf |buf, buflen| {
764+
llvm::LLVMConstStringInContext(cx.llcx, buf as *c_char, buflen as c_uint, False)
765765
};
766766

767767
let gsym = token::gensym("str");

src/librustc/middle/trans/tvec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ pub fn trans_lit_str(bcx: @mut Block,
265265
Ignore => bcx,
266266
SaveIn(lldest) => {
267267
unsafe {
268-
let bytes = str_lit.len(); // count null-terminator too
268+
let bytes = str_lit.len();
269269
let llbytes = C_uint(bcx.ccx(), bytes);
270270
let llcstr = C_cstr(bcx.ccx(), str_lit);
271271
let llcstr = llvm::LLVMConstPointerCast(llcstr, Type::i8p().to_ref());

0 commit comments

Comments
 (0)