Skip to content

Commit 701f892

Browse files
committed
Use \xff\0 as terminator
This prevents people depending on nul-termination.
1 parent 025d3c1 commit 701f892

File tree

8 files changed

+41
-39
lines changed

8 files changed

+41
-39
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ pub(crate) fn alloc_type_name<'tcx>(
2828
tcx: TyCtxt<'tcx>,
2929
ty: Ty<'tcx>,
3030
) -> (ConstAllocation<'tcx>, u64) {
31-
let mut path = crate::util::type_name(tcx, ty);
31+
let mut path = crate::util::type_name(tcx, ty).into_bytes();
3232
let path_len = path.len().try_into().unwrap();
33-
if !path.contains('\0') {
34-
path.push('\0');
33+
if !path.contains(&0) {
34+
path.extend(b"\xff\0");
3535
};
36-
let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes(), ());
36+
let alloc = Allocation::from_bytes_byte_aligned_immutable(path, ());
3737
(tcx.mk_const_alloc(alloc), path_len)
3838
}
3939

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ where
10211021
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
10221022
let ptr = if !s.contains('\0') {
10231023
let mut bytes = s.as_bytes().to_owned();
1024-
bytes.push(0);
1024+
bytes.extend(b"\xff\0");
10251025
self.allocate_bytes_dedup(&bytes)?
10261026
} else {
10271027
self.allocate_bytes_dedup(s.as_bytes())?

compiler/rustc_mir_build/src/builder/expr/as_constant.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ fn lit_to_mir_constant<'tcx>(tcx: TyCtxt<'tcx>, lit_input: LitToConstInput<'tcx>
122122
(ast::LitKind::Str(s, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => {
123123
let s = s.as_str();
124124
let allocation = if !s.contains('\0') {
125-
let mut s = s.to_owned();
126-
s.push('\0');
127-
Allocation::from_bytes_byte_aligned_immutable(s.as_bytes(), ())
125+
let mut s = s.as_bytes().to_owned();
126+
s.extend(b"\xff\0");
127+
Allocation::from_bytes_byte_aligned_immutable(s, ())
128128
} else {
129129
Allocation::from_bytes_byte_aligned_immutable(s.as_bytes(), ())
130130
};

tests/codegen/remap_path_prefix/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod aux_mod;
1212
include!("aux_mod.rs");
1313

1414
// Here we check that the expansion of the file!() macro is mapped.
15-
// CHECK: @alloc_4079a2e7607f89f86df6b8a72ba0dd06 = private unnamed_addr constant [35 x i8] c"/the/src/remap_path_prefix/main.rs\00"
15+
// CHECK: @alloc_643660180b5bd639a42b5b1707ce11a5 = private unnamed_addr constant [36 x i8] c"/the/src/remap_path_prefix/main.rs\FF\00"
1616
pub static FILE_PATH: &'static str = file!();
1717

1818
fn main() {

tests/mir-opt/const_allocation.main.GVN.after.32bit.mir

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,27 @@ ALLOC2 (size: 16, align: 4) {
3333
╾ALLOC4<imm>╼ 03 00 00 00 ╾ALLOC5<imm>╼ 03 00 00 00 │ ╾──╼....╾──╼....
3434
}
3535

36-
ALLOC4 (size: 4, align: 1) {
37-
66 6f 6f 00 │ foo.
36+
ALLOC4 (size: 5, align: 1) {
37+
66 6f 6f ff 00 │ foo..
3838
}
3939

40-
ALLOC5 (size: 4, align: 1) {
41-
62 61 72 00 │ bar.
40+
ALLOC5 (size: 5, align: 1) {
41+
62 61 72 ff 00 │ bar..
4242
}
4343

4444
ALLOC3 (size: 24, align: 4) {
4545
0x00 │ ╾ALLOC6<imm>╼ 03 00 00 00 ╾ALLOC7<imm>╼ 03 00 00 00 │ ╾──╼....╾──╼....
4646
0x10 │ ╾ALLOC8<imm>╼ 04 00 00 00 │ ╾──╼....
4747
}
4848

49-
ALLOC6 (size: 4, align: 1) {
50-
6d 65 68 00 │ meh.
49+
ALLOC6 (size: 5, align: 1) {
50+
6d 65 68 ff 00 │ meh..
5151
}
5252

53-
ALLOC7 (size: 4, align: 1) {
54-
6d 6f 70 00 │ mop.
53+
ALLOC7 (size: 5, align: 1) {
54+
6d 6f 70 ff 00 │ mop..
5555
}
5656

57-
ALLOC8 (size: 5, align: 1) {
58-
6d c3 b6 70 00 │ m..p.
57+
ALLOC8 (size: 6, align: 1) {
58+
6d c3 b6 70 ff 00 │ m..p..
5959
}

tests/mir-opt/const_allocation.main.GVN.after.64bit.mir

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ ALLOC2 (size: 32, align: 8) {
3636
0x10 │ ╾ALLOC5<imm>╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........
3737
}
3838

39-
ALLOC4 (size: 4, align: 1) {
40-
66 6f 6f 00 │ foo.
39+
ALLOC4 (size: 5, align: 1) {
40+
66 6f 6f ff 00 │ foo..
4141
}
4242

43-
ALLOC5 (size: 4, align: 1) {
44-
62 61 72 00 │ bar.
43+
ALLOC5 (size: 5, align: 1) {
44+
62 61 72 ff 00 │ bar..
4545
}
4646

4747
ALLOC3 (size: 48, align: 8) {
@@ -50,14 +50,14 @@ ALLOC3 (size: 48, align: 8) {
5050
0x20 │ ╾ALLOC8<imm>╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........
5151
}
5252

53-
ALLOC6 (size: 4, align: 1) {
54-
6d 65 68 00 │ meh.
53+
ALLOC6 (size: 5, align: 1) {
54+
6d 65 68 ff 00 │ meh..
5555
}
5656

57-
ALLOC7 (size: 4, align: 1) {
58-
6d 6f 70 00 │ mop.
57+
ALLOC7 (size: 5, align: 1) {
58+
6d 6f 70 ff 00 │ mop..
5959
}
6060

61-
ALLOC8 (size: 5, align: 1) {
62-
6d c3 b6 70 00 │ m..p.
61+
ALLOC8 (size: 6, align: 1) {
62+
6d c3 b6 70 ff 00 │ m..p..
6363
}

tests/ui-fulldeps/stable-mir/check_allocation.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ fn check_bar(item: CrateItem) {
7878

7979
let alloc_id_0 = alloc.provenance.ptrs[0].1.0;
8080
let GlobalAlloc::Memory(allocation) = GlobalAlloc::from(alloc_id_0) else { unreachable!() };
81-
assert_eq!(allocation.bytes.len(), 4);
81+
assert_eq!(allocation.bytes.len(), 5);
8282
assert_eq!(allocation.bytes[0].unwrap(), Char::CapitalB.to_u8());
8383
assert_eq!(allocation.bytes[1].unwrap(), Char::SmallA.to_u8());
8484
assert_eq!(allocation.bytes[2].unwrap(), Char::SmallR.to_u8());
85-
assert_eq!(allocation.bytes[3].unwrap(), Char::Null.to_u8());
86-
assert_eq!(std::str::from_utf8(&allocation.raw_bytes().unwrap()), Ok("Bar\0"));
85+
assert_eq!(allocation.bytes[3].unwrap(), 0xff);
86+
assert_eq!(allocation.bytes[4].unwrap(), Char::Null.to_u8());
87+
assert_eq!(allocation.raw_bytes().unwrap(), b"Bar\xff\0");
8788
}
8889

8990
/// Check the allocation data for static `C_STR`.

tests/ui-fulldeps/stable-mir/check_transform.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ extern crate rustc_driver;
1717
extern crate rustc_interface;
1818
extern crate stable_mir;
1919

20+
use std::convert::TryFrom;
21+
use std::io::Write;
22+
use std::ops::ControlFlow;
23+
2024
use stable_mir::mir::alloc::GlobalAlloc;
2125
use stable_mir::mir::mono::Instance;
2226
use stable_mir::mir::{Body, ConstOperand, Operand, Rvalue, StatementKind, TerminatorKind};
2327
use stable_mir::ty::{ConstantKind, MirConst};
2428
use stable_mir::{CrateDef, CrateItems, ItemKind};
25-
use std::convert::TryFrom;
26-
use std::io::Write;
27-
use std::ops::ControlFlow;
2829

2930
const CRATE_NAME: &str = "input";
3031

@@ -37,17 +38,17 @@ fn test_transform() -> ControlFlow<()> {
3738
let target_fn = *get_item(&items, (ItemKind::Fn, "dummy")).unwrap();
3839
let instance = Instance::try_from(target_fn).unwrap();
3940
let body = instance.body().unwrap();
40-
check_msg(&body, "oops\0");
41+
check_msg(&body, b"oops\xff\0");
4142

4243
let new_msg = "new panic message";
4344
let new_body = change_panic_msg(body, new_msg);
44-
check_msg(&new_body, new_msg);
45+
check_msg(&new_body, new_msg.as_bytes());
4546

4647
ControlFlow::Continue(())
4748
}
4849

4950
/// Check that the body panic message matches the given message.
50-
fn check_msg(body: &Body, expected: &str) {
51+
fn check_msg(body: &Body, expected: &[u8]) {
5152
let msg = body
5253
.blocks
5354
.iter()
@@ -80,7 +81,7 @@ fn check_msg(body: &Body, expected: &str) {
8081
unreachable!()
8182
};
8283
let bytes = val.raw_bytes().unwrap();
83-
Some(std::str::from_utf8(&bytes).unwrap().to_string())
84+
Some(bytes.to_owned())
8485
}
8586
_ => None,
8687
})

0 commit comments

Comments
 (0)