Skip to content

Commit d60438f

Browse files
committed
Validate pipe in TryFrom<OwnedHandle> for Pipe*
Signed-off-by: Jiahao XU <[email protected]>
1 parent 97626b6 commit d60438f

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

library/std/src/sys/anonymous_pipe.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ mod windows {
223223
RawHandle,
224224
},
225225
sys::{
226+
c::{GetFileType, FILE_TYPE_PIPE},
226227
handle::Handle,
227228
pipe::{anon_pipe, AnonPipe, Pipes},
228229
},
@@ -279,16 +280,20 @@ mod windows {
279280
impl_traits!(PipeReader);
280281
impl_traits!(PipeWriter);
281282

282-
fn owned_handle_to_anon_pipe(owned_handle: OwnedHandle) -> AnonPipe {
283-
AnonPipe::from_inner(Handle::from_inner(owned_handle))
283+
fn convert_to_pipe(owned_handle: OwnedHandle) -> io::Result<AnonPipe> {
284+
if unsafe { GetFileType(owned_handle.as_raw_handle()) } == FILE_TYPE_PIPE {
285+
Ok(AnonPipe::from_inner(Handle::from_inner(owned_handle)))
286+
} else {
287+
Err(io::Error::new(io::ErrorKind::InvalidInput, "Not a pipe"))
288+
}
284289
}
285290

286291
#[unstable(feature = "anonymous_pipe", issue = "127154")]
287292
impl TryFrom<OwnedHandle> for PipeReader {
288293
type Error = io::Error;
289294

290295
fn try_from(owned_handle: OwnedHandle) -> Result<Self, Self::Error> {
291-
Ok(Self(owned_handle_to_anon_pipe(owned_handle)))
296+
convert_to_pipe(owned_handle).map(Self)
292297
}
293298
}
294299

@@ -297,7 +302,7 @@ mod windows {
297302
type Error = io::Error;
298303

299304
fn try_from(owned_handle: OwnedHandle) -> Result<Self, Self::Error> {
300-
Ok(Self(owned_handle_to_anon_pipe(owned_handle)))
305+
convert_to_pipe(owned_handle).map(Self)
301306
}
302307
}
303308
}

0 commit comments

Comments
 (0)