Skip to content

Commit 54ff463

Browse files
authored
Merge pull request #4074 from tiif/cleanup
cleanup: avoid passing byte slice to anonsocket_read
2 parents 599aef2 + 16d549a commit 54ff463

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/tools/miri/src/shims/unix/unnamed_socket.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,9 @@ impl FileDescription for AnonSocket {
9090
dest: &MPlaceTy<'tcx>,
9191
ecx: &mut MiriInterpCx<'tcx>,
9292
) -> InterpResult<'tcx> {
93-
let mut bytes = vec![0; len];
94-
9593
// Always succeed on read size 0.
9694
if len == 0 {
97-
return ecx.return_read_success(ptr, &bytes, 0, dest);
95+
return ecx.return_read_success(ptr, &[], 0, dest);
9896
}
9997

10098
let Some(readbuf) = &self.readbuf else {
@@ -106,7 +104,7 @@ impl FileDescription for AnonSocket {
106104
if self.peer_fd().upgrade().is_none() {
107105
// Socketpair with no peer and empty buffer.
108106
// 0 bytes successfully read indicates end-of-file.
109-
return ecx.return_read_success(ptr, &bytes, 0, dest);
107+
return ecx.return_read_success(ptr, &[], 0, dest);
110108
} else {
111109
if self.is_nonblock {
112110
// Non-blocking socketpair with writer and empty buffer.
@@ -123,7 +121,7 @@ impl FileDescription for AnonSocket {
123121
}
124122
}
125123
// TODO: We might need to decide what to do if peer_fd is closed when read is blocked.
126-
anonsocket_read(self, self.peer_fd().upgrade(), &mut bytes, ptr, dest, ecx)
124+
anonsocket_read(self, self.peer_fd().upgrade(), len, ptr, dest, ecx)
127125
}
128126

129127
fn write<'tcx>(
@@ -211,11 +209,13 @@ fn anonsocket_write<'tcx>(
211209
fn anonsocket_read<'tcx>(
212210
anonsocket: &AnonSocket,
213211
peer_fd: Option<FileDescriptionRef>,
214-
bytes: &mut [u8],
212+
len: usize,
215213
ptr: Pointer,
216214
dest: &MPlaceTy<'tcx>,
217215
ecx: &mut MiriInterpCx<'tcx>,
218216
) -> InterpResult<'tcx> {
217+
let mut bytes = vec![0; len];
218+
219219
let Some(readbuf) = &anonsocket.readbuf else {
220220
// FIXME: This should return EBADF, but there's no nice way to do that as there's no
221221
// corresponding ErrorKind variant.
@@ -230,7 +230,7 @@ fn anonsocket_read<'tcx>(
230230

231231
// Do full read / partial read based on the space available.
232232
// Conveniently, `read` exists on `VecDeque` and has exactly the desired behavior.
233-
let actual_read_size = readbuf.buf.read(bytes).unwrap();
233+
let actual_read_size = readbuf.buf.read(&mut bytes[..]).unwrap();
234234

235235
// Need to drop before others can access the readbuf again.
236236
drop(readbuf);
@@ -246,7 +246,7 @@ fn anonsocket_read<'tcx>(
246246
ecx.check_and_update_readiness(&peer_fd)?;
247247
}
248248

249-
ecx.return_read_success(ptr, bytes, actual_read_size, dest)
249+
ecx.return_read_success(ptr, &bytes, actual_read_size, dest)
250250
}
251251

252252
impl UnixFileDescription for AnonSocket {

0 commit comments

Comments
 (0)