Skip to content

Commit

Permalink
Fix crash due to rust-lang/rust#124210 on rust 180
Browse files Browse the repository at this point in the history
  • Loading branch information
abezukor committed Aug 6, 2024
1 parent cf6ff29 commit 2e5ff38
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions src/corebluetooth/l2cap_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
task::{Context, Poll},
};

use objc_foundation::INSData;
use objc_foundation::{INSData, NSData};
use objc_id::{Id, Shared};
use tokio::{
io::{AsyncRead, AsyncWrite, ReadBuf},
Expand Down Expand Up @@ -33,8 +33,7 @@ pub struct Channel {

enum ChannelCreationError {
FileDescriptorPropertyNotValid,
InputFileDescriptorBytesWrongSize,
OutputFileDescriptorBytesWrongSize,
FileDescriptorBytesWrongSize,
FileDescriptorsNotIdentical,
SetNonBlockingModeFailed(std::io::Error),
TokioStreamCreation(std::io::Error),
Expand All @@ -45,27 +44,21 @@ impl Channel {
let input_stream = channel.input_stream();
let output_stream = channel.output_stream();

let in_stream_prop = input_stream.property(&unsafe { kCFStreamPropertySocketNativeHandle });
let out_stream_prop = output_stream.property(&unsafe { kCFStreamPropertySocketNativeHandle });
let in_stream_prop = input_stream.property(&unsafe { kCFStreamPropertySocketNativeHandle }).map(NSData::bytes);
let out_stream_prop = output_stream.property(&unsafe { kCFStreamPropertySocketNativeHandle }).map(NSData::bytes);

let (Some(in_data), Some(out_data)) = (in_stream_prop, out_stream_prop) else {
return Err(ChannelCreationError::FileDescriptorPropertyNotValid.into());
};
let in_bytes = in_data
.bytes()
.try_into()
.map_err(|_| ChannelCreationError::InputFileDescriptorBytesWrongSize)?;
let in_fd = RawFd::from_ne_bytes(in_bytes);

let out_bytes = out_data
.bytes()
.try_into()
.map_err(|_| ChannelCreationError::OutputFileDescriptorBytesWrongSize)?;
let out_fd = RawFd::from_ne_bytes(out_bytes);

if in_fd != out_fd {

if in_data != out_data {
return Err(ChannelCreationError::FileDescriptorsNotIdentical.into());
};

let in_fd = RawFd::from_ne_bytes(
in_data.try_into()
.map_err(|_| ChannelCreationError::FileDescriptorBytesWrongSize)?
);

let stream = unsafe { std::os::unix::net::UnixStream::from_raw_fd(in_fd) };
stream
Expand Down Expand Up @@ -106,12 +99,9 @@ impl From<ChannelCreationError> for Error {
fn from(value: ChannelCreationError) -> Self {
let message = match &value {
ChannelCreationError::FileDescriptorPropertyNotValid => "File descriptor property not valid.",
ChannelCreationError::InputFileDescriptorBytesWrongSize => {
ChannelCreationError::FileDescriptorBytesWrongSize => {
"Input file descriptor bytes are an invalid size."
}
ChannelCreationError::OutputFileDescriptorBytesWrongSize => {
"Output file descriptor bytes are an invalid size."
}
ChannelCreationError::FileDescriptorsNotIdentical => "Input and output file descriptors are not identical.",
ChannelCreationError::SetNonBlockingModeFailed(_) => "Could not get convert socket to async.",
ChannelCreationError::TokioStreamCreation(_) => "Failed to create tokio unix socket.",
Expand All @@ -121,8 +111,7 @@ impl From<ChannelCreationError> for Error {
ErrorKind::Internal,
match value {
ChannelCreationError::FileDescriptorPropertyNotValid
| ChannelCreationError::InputFileDescriptorBytesWrongSize
| ChannelCreationError::OutputFileDescriptorBytesWrongSize
| ChannelCreationError::FileDescriptorBytesWrongSize
| ChannelCreationError::FileDescriptorsNotIdentical => None,
ChannelCreationError::SetNonBlockingModeFailed(src)
| ChannelCreationError::TokioStreamCreation(src) => Some(Box::new(src)),
Expand Down

0 comments on commit 2e5ff38

Please sign in to comment.