Skip to content

Commit 37a9885

Browse files
committed
auto merge of #13291 : thestinger/rust/no_null, r=alexcrichton
This was missed when dropping the null-termination from our string types. An explicit null byte can still be placed anywhere in a string if desired, but there's no reason to stick one at the end of every string constant.
2 parents f819c21 + 7ce2630 commit 37a9885

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
lines changed

src/librustc/middle/trans/common.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ pub fn C_u8(ccx: &CrateContext, i: uint) -> ValueRef {
565565

566566
// This is a 'c-like' raw string, which differs from
567567
// our boxed-and-length-annotated strings.
568-
pub fn C_cstr(cx: &CrateContext, s: InternedString) -> ValueRef {
568+
pub fn C_cstr(cx: &CrateContext, s: InternedString, null_terminated: bool) -> ValueRef {
569569
unsafe {
570570
match cx.const_cstr_cache.borrow().find(&s) {
571571
Some(&llval) => return llval,
@@ -575,7 +575,7 @@ pub fn C_cstr(cx: &CrateContext, s: InternedString) -> ValueRef {
575575
let sc = llvm::LLVMConstStringInContext(cx.llcx,
576576
s.get().as_ptr() as *c_char,
577577
s.get().len() as c_uint,
578-
False);
578+
!null_terminated as Bool);
579579

580580
let gsym = token::gensym("str");
581581
let g = format!("str{}", gsym).with_c_str(|buf| {
@@ -595,7 +595,7 @@ pub fn C_cstr(cx: &CrateContext, s: InternedString) -> ValueRef {
595595
pub fn C_str_slice(cx: &CrateContext, s: InternedString) -> ValueRef {
596596
unsafe {
597597
let len = s.get().len();
598-
let cs = llvm::LLVMConstPointerCast(C_cstr(cx, s), Type::i8p(cx).to_ref());
598+
let cs = llvm::LLVMConstPointerCast(C_cstr(cx, s, false), Type::i8p(cx).to_ref());
599599
C_struct(cx, [cs, C_uint(cx, len)], false)
600600
}
601601
}
@@ -900,7 +900,7 @@ pub fn filename_and_line_num_from_span(bcx: &Block, span: Span)
900900
-> (ValueRef, ValueRef) {
901901
let loc = bcx.sess().codemap().lookup_char_pos(span.lo);
902902
let filename_cstr = C_cstr(bcx.ccx(),
903-
token::intern_and_get_ident(loc.file.name));
903+
token::intern_and_get_ident(loc.file.name), true);
904904
let filename = build::PointerCast(bcx, filename_cstr, Type::i8p(bcx.ccx()));
905905
let line = C_int(bcx.ccx(), loc.line as int);
906906
(filename, line)

src/librustc/middle/trans/controlflow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,10 @@ pub fn trans_fail<'a>(
331331
fail_str: InternedString)
332332
-> &'a Block<'a> {
333333
let ccx = bcx.ccx();
334-
let v_fail_str = C_cstr(ccx, fail_str);
334+
let v_fail_str = C_cstr(ccx, fail_str, true);
335335
let _icx = push_ctxt("trans_fail_value");
336336
let loc = bcx.sess().codemap().lookup_char_pos(sp.lo);
337-
let v_filename = C_cstr(ccx, token::intern_and_get_ident(loc.file.name));
337+
let v_filename = C_cstr(ccx, token::intern_and_get_ident(loc.file.name), true);
338338
let v_line = loc.line as int;
339339
let v_str = PointerCast(bcx, v_fail_str, Type::i8p(ccx));
340340
let v_filename = PointerCast(bcx, v_filename, Type::i8p(ccx));

src/librustc/middle/trans/debuginfo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2129,7 +2129,7 @@ fn type_metadata(cx: &CrateContext,
21292129
let i8_t = ty::mk_i8();
21302130
match *vstore {
21312131
ty::vstore_fixed(len) => {
2132-
fixed_vec_metadata(cx, i8_t, len + 1, usage_site_span)
2132+
fixed_vec_metadata(cx, i8_t, len, usage_site_span)
21332133
},
21342134
ty::vstore_uniq => {
21352135
let vec_metadata = vec_metadata(cx, i8_t, usage_site_span);

src/librustc/middle/trans/reflect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<'a> Reflector<'a> {
5858
let str_ty = ty::mk_str(bcx.tcx(), str_vstore);
5959
let scratch = rvalue_scratch_datum(bcx, str_ty, "");
6060
let len = C_uint(bcx.ccx(), s.get().len());
61-
let c_str = PointerCast(bcx, C_cstr(bcx.ccx(), s), Type::i8p(bcx.ccx()));
61+
let c_str = PointerCast(bcx, C_cstr(bcx.ccx(), s, false), Type::i8p(bcx.ccx()));
6262
Store(bcx, c_str, GEPi(bcx, scratch.val, [ 0, 0 ]));
6363
Store(bcx, len, GEPi(bcx, scratch.val, [ 0, 1 ]));
6464
scratch.val

src/librustc/middle/trans/tvec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ pub fn trans_lit_str<'a>(
272272
unsafe {
273273
let bytes = str_lit.get().len();
274274
let llbytes = C_uint(bcx.ccx(), bytes);
275-
let llcstr = C_cstr(bcx.ccx(), str_lit);
275+
let llcstr = C_cstr(bcx.ccx(), str_lit, false);
276276
let llcstr = llvm::LLVMConstPointerCast(llcstr, Type::i8p(bcx.ccx()).to_ref());
277277
Store(bcx, llcstr,
278278
GEPi(bcx, lldest, [0u, abi::slice_elt_base]));
@@ -302,7 +302,7 @@ pub fn trans_uniq_vstore<'a>(bcx: &'a Block<'a>,
302302
ast::ExprLit(lit) => {
303303
match lit.node {
304304
ast::LitStr(ref s, _) => {
305-
let llptrval = C_cstr(bcx.ccx(), (*s).clone());
305+
let llptrval = C_cstr(bcx.ccx(), (*s).clone(), false);
306306
let llptrval = PointerCast(bcx,
307307
llptrval,
308308
Type::i8p(bcx.ccx()));
@@ -376,7 +376,7 @@ pub fn write_content<'a>(
376376
SaveIn(lldest) => {
377377
let bytes = s.get().len();
378378
let llbytes = C_uint(bcx.ccx(), bytes);
379-
let llcstr = C_cstr(bcx.ccx(), (*s).clone());
379+
let llcstr = C_cstr(bcx.ccx(), (*s).clone(), false);
380380
base::call_memcpy(bcx,
381381
lldest,
382382
llcstr,

src/test/debug-info/include_string.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
// debugger:rbreak zzz
1515
// debugger:run
1616
// debugger:finish
17-
// debugger:print string1
18-
// check:$1 = [...]"some text to include in another file as string 1", length = 48}
19-
// debugger:print string2
20-
// check:$2 = [...]"some text to include in another file as string 2", length = 48}
21-
// debugger:print string3
22-
// check:$3 = [...]"some text to include in another file as string 3", length = 48}
17+
// debugger:print string1.length
18+
// check:$1 = 48
19+
// debugger:print string2.length
20+
// check:$2 = 48
21+
// debugger:print string3.length
22+
// check:$3 = 48
2323
// debugger:continue
2424

2525
#[allow(unused_variable)];

0 commit comments

Comments
 (0)