]> git.lizzy.rs Git - rust.git/commitdiff
Remove inappropriate .to_c_str() in C_cstr()
authorKevin Ballard <kevin@sb.org>
Thu, 15 Aug 2013 06:40:28 +0000 (23:40 -0700)
committerKevin Ballard <kevin@sb.org>
Thu, 15 Aug 2013 08:33:10 +0000 (01:33 -0700)
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".

src/librustc/middle/trans/common.rs
src/librustc/middle/trans/tvec.rs

index c5ffcccdb4646ad53a46bbaa5065089d6a60f560..c2f7986a2508442b3d09dc08d75acd0160feef3a 100644 (file)
@@ -36,7 +36,7 @@
 use std::cast::transmute;
 use std::cast;
 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 syntax::ast::ident;
 use syntax::ast_map::{path, path_elt};
@@ -760,8 +760,8 @@ pub fn C_cstr(cx: &mut CrateContext, s: @str) -> ValueRef {
             None => ()
         }
 
-        let sc = do s.with_c_str |buf| {
-            llvm::LLVMConstStringInContext(cx.llcx, buf, s.len() as c_uint, False)
+        let sc = do s.as_imm_buf |buf, buflen| {
+            llvm::LLVMConstStringInContext(cx.llcx, buf as *c_char, buflen as c_uint, False)
         };
 
         let gsym = token::gensym("str");
index 5b0e2fa18f240af3716c6604a6ae391512b18d5e..47d5b7ff3a53f9ae5b72793068a0b84f919b3fd8 100644 (file)
@@ -265,7 +265,7 @@ pub fn trans_lit_str(bcx: @mut Block,
         Ignore => bcx,
         SaveIn(lldest) => {
             unsafe {
-                let bytes = str_lit.len(); // count null-terminator too
+                let bytes = str_lit.len();
                 let llbytes = C_uint(bcx.ccx(), bytes);
                 let llcstr = C_cstr(bcx.ccx(), str_lit);
                 let llcstr = llvm::LLVMConstPointerCast(llcstr, Type::i8p().to_ref());