Skip to content

Commit 3cd7a17

Browse files
authored
Rollup merge of rust-lang#65704 - RalfJung:exact-size, r=oli-obk
relax ExactSizeIterator bound on write_bytes Too many iterators don't have that bound. Instead we do run-time checks. r? @oli-obk
2 parents b2b3579 + fe84809 commit 3cd7a17

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/librustc/mir/interpret/allocation.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,14 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
353353
&mut self,
354354
cx: &impl HasDataLayout,
355355
ptr: Pointer<Tag>,
356-
src: impl IntoIterator<Item=u8, IntoIter: iter::ExactSizeIterator>,
356+
src: impl IntoIterator<Item=u8>,
357357
) -> InterpResult<'tcx>
358358
{
359359
let mut src = src.into_iter();
360-
let bytes = self.get_bytes_mut(cx, ptr, Size::from_bytes(src.len() as u64))?;
360+
let (lower, upper) = src.size_hint();
361+
let len = upper.expect("can only write bounded iterators");
362+
assert_eq!(lower, len, "can only write iterators with a precise length");
363+
let bytes = self.get_bytes_mut(cx, ptr, Size::from_bytes(len as u64))?;
361364
// `zip` would stop when the first iterator ends; we want to definitely
362365
// cover all of `bytes`.
363366
for dest in bytes {

src/librustc_mir/interpret/memory.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! short-circuiting the empty case!
88
99
use std::collections::VecDeque;
10-
use std::{ptr, iter};
10+
use std::ptr;
1111
use std::borrow::Cow;
1212

1313
use rustc::ty::{self, Instance, ParamEnv, query::TyCtxtAt};
@@ -791,11 +791,12 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
791791
pub fn write_bytes(
792792
&mut self,
793793
ptr: Scalar<M::PointerTag>,
794-
src: impl IntoIterator<Item=u8, IntoIter: iter::ExactSizeIterator>,
794+
src: impl IntoIterator<Item=u8>,
795795
) -> InterpResult<'tcx>
796796
{
797797
let src = src.into_iter();
798-
let size = Size::from_bytes(src.len() as u64);
798+
let size = Size::from_bytes(src.size_hint().0 as u64);
799+
// `write_bytes` checks that this lower bound matches the upper bound matches reality.
799800
let ptr = match self.check_ptr_access(ptr, size, Align::from_bytes(1).unwrap())? {
800801
Some(ptr) => ptr,
801802
None => return Ok(()), // zero-sized access

0 commit comments

Comments
 (0)