Skip to content

Commit ceeb9bd

Browse files
authored
Rollup merge of #72128 - RalfJung:str-validity, r=oli-obk
strings do not have to be valid UTF-8 any more Cc rust-lang/reference#792 r? @oli-obk
2 parents 31e5be0 + 6f9810c commit ceeb9bd

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

src/librustc_mir/interpret/validity.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::mir::interpret::{InterpError, InterpErrorInfo};
1515
use rustc_middle::ty;
1616
use rustc_middle::ty::layout::TyAndLayout;
1717
use rustc_span::symbol::{sym, Symbol};
18-
use rustc_target::abi::{Abi, LayoutOf, Scalar, VariantIdx, Variants};
18+
use rustc_target::abi::{Abi, LayoutOf, Scalar, Size, VariantIdx, Variants};
1919

2020
use std::hash::Hash;
2121

@@ -744,10 +744,11 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
744744
match op.layout.ty.kind {
745745
ty::Str => {
746746
let mplace = op.assert_mem_place(self.ecx); // strings are never immediate
747+
let len = mplace.len(self.ecx)?;
747748
try_validation!(
748-
self.ecx.read_str(mplace),
749+
self.ecx.memory.read_bytes(mplace.ptr, Size::from_bytes(len)),
749750
self.path,
750-
err_ub!(InvalidStr(..)) => { "uninitialized or non-UTF-8 data in str" },
751+
err_ub!(InvalidUninitBytes(..)) => { "uninitialized data in `str`" },
751752
);
752753
}
753754
ty::Array(tys, ..) | ty::Slice(tys)

src/test/ui/consts/const-eval/ub-wide-ptr.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ const MY_STR_LENGTH_PTR: &MyStr = unsafe { mem::transmute((&42u8, &3)) };
4242
const MY_STR_MUCH_TOO_LONG: &MyStr = unsafe { mem::transmute((&42u8, usize::MAX)) };
4343
//~^ ERROR it is undefined behavior to use this value
4444

45-
// invalid UTF-8
46-
const STR_NO_UTF8: &str = unsafe { mem::transmute::<&[u8], _>(&[0xFF]) };
45+
// uninitialized byte
46+
const STR_NO_INIT: &str = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
4747
//~^ ERROR it is undefined behavior to use this value
48-
// invalid UTF-8 in user-defined str-like
49-
const MYSTR_NO_UTF8: &MyStr = unsafe { mem::transmute::<&[u8], _>(&[0xFF]) };
48+
// uninitialized byte in user-defined str-like
49+
const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
5050
//~^ ERROR it is undefined behavior to use this value
5151

5252
// # slice

src/test/ui/consts/const-eval/ub-wide-ptr.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ LL | const MY_STR_MUCH_TOO_LONG: &MyStr = unsafe { mem::transmute((&42u8, usize:
4141
error[E0080]: it is undefined behavior to use this value
4242
--> $DIR/ub-wide-ptr.rs:46:1
4343
|
44-
LL | const STR_NO_UTF8: &str = unsafe { mem::transmute::<&[u8], _>(&[0xFF]) };
45-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized or non-UTF-8 data in str at .<deref>
44+
LL | const STR_NO_INIT: &str = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
45+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized data in `str` at .<deref>
4646
|
4747
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
4848

4949
error[E0080]: it is undefined behavior to use this value
5050
--> $DIR/ub-wide-ptr.rs:49:1
5151
|
52-
LL | const MYSTR_NO_UTF8: &MyStr = unsafe { mem::transmute::<&[u8], _>(&[0xFF]) };
53-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized or non-UTF-8 data in str at .<deref>.0
52+
LL | const MYSTR_NO_INIT: &MyStr = unsafe { mem::transmute::<&[_], _>(&[MaybeUninit::<u8> { uninit: () }]) };
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized data in `str` at .<deref>.0
5454
|
5555
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
5656

0 commit comments

Comments
 (0)