1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! FFI helper functions, traits and types to communicate with `sentry-native`.

#[cfg(target_os = "windows")]
use std::os::windows::ffi::OsStrExt;
#[cfg(doc)]
use std::process::abort;
use std::{
    ffi::{CStr, CString},
    os::raw::c_char,
    panic::{self, AssertUnwindSafe},
    path::PathBuf,
    process,
};

#[cfg(not(target_os = "windows"))]
use std::{mem, os::unix::ffi::OsStringExt};

/// Cross-platform return type for [`CPath::into_os_vec`].
#[cfg(target_os = "windows")]
type COsString = u16;
/// Cross-platform return type for [`CPath::into_os_vec`].
#[cfg(not(target_os = "windows"))]
type COsString = c_char;

/// Helper trait to convert [`PathBuf`] to `Vec<COsString>`.
pub trait CPath {
    /// Re-encodes `self` into an OS compatible `Vec<COsString>`.
    fn into_os_vec(self) -> Vec<COsString>;
}

impl CPath for PathBuf {
    fn into_os_vec(self) -> Vec<COsString> {
        let path = self.into_os_string();

        #[cfg(target_os = "windows")]
        let path = path.encode_wide();
        #[cfg(all(
            not(target_os = "windows"),
            not(all(target_os = "linux", target_arch = "aarch64"))
        ))]
        let path = path
            .into_vec()
            .into_iter()
            .map(|ch| unsafe { mem::transmute::<u8, i8>(ch) });
        #[cfg(all(target_os = "linux", target_arch = "aarch64"))]
        let path = path.into_vec().into_iter();

        path.take_while(|ch| *ch != 0).chain(Some(0)).collect()
    }
}

/// Helper trait to convert `*const c_char` to [`str`].
pub trait CToR {
    /// Yields a [`str`] from `self`.
    ///
    /// # Panics
    /// Panics if `self` contains any invalid UTF-8.
    ///
    /// # Safety
    /// The same safety issues apply as in [`CStr::from_ptr`], except the null
    /// pointer check, but the main concern is the lifetime of the pointer.
    #[allow(clippy::wrong_self_convention)]
    unsafe fn as_str<'a>(self) -> Option<&'a str>;
}

impl CToR for *const c_char {
    #[allow(unused_unsafe)]
    unsafe fn as_str<'a>(self) -> Option<&'a str> {
        if self.is_null() {
            None
        } else {
            Some(
                unsafe { CStr::from_ptr(self) }
                    .to_str()
                    .expect("found invalid UTF-8"),
            )
        }
    }
}

/// Helper trait to convert [`String`] to [`CString`].
pub trait RToC {
    /// Re-encodes `self` into a [`CString`].
    fn into_cstring(self) -> CString;
}

impl RToC for String {
    fn into_cstring(mut self) -> CString {
        if let Some(position) = self.find('\0') {
            self.truncate(position);
        }

        CString::new(self).expect("found null byte")
    }
}

/// Catch unwinding panics and [`abort`] if any occured.
pub fn catch<R>(fun: impl FnOnce() -> R) -> R {
    match panic::catch_unwind(AssertUnwindSafe(fun)) {
        Ok(ret) => ret,
        Err(_) => process::abort(),
    }
}

#[cfg(test)]
mod cpath {
    #![allow(clippy::non_ascii_literal)]

    use crate::CPath;
    #[cfg(target_os = "windows")]
    use std::os::windows::ffi::OsStringExt;
    use std::{ffi::OsString, path::PathBuf};
    #[cfg(not(target_os = "windows"))]
    use std::{mem, os::unix::ffi::OsStringExt};

    fn convert(string: &str) -> OsString {
        let path = PathBuf::from(string.to_owned()).into_os_vec();

        #[cfg(target_os = "windows")]
        {
            OsString::from_wide(&path[..])
        }
        #[cfg(all(
            not(target_os = "windows"),
            not(all(target_os = "linux", target_arch = "aarch64"))
        ))]
        {
            OsString::from_vec(
                path.into_iter()
                    .map(|ch| unsafe { mem::transmute::<i8, u8>(ch) })
                    .collect(),
            )
        }
        #[cfg(all(target_os = "linux", target_arch = "aarch64"))]
        {
            OsString::from_vec(path.into_iter().collect())
        }
    }

    #[test]
    fn valid() {
        assert_eq!("abcdefgh\0", convert("abcdefgh"));
        assert_eq!("🤦‍♂️🤦‍♀️🤷‍♂️🤷‍♀️\0", convert("🤦‍♂️🤦‍♀️🤷‍♂️🤷‍♀️"));
        assert_eq!("abcdefgh\0", convert("abcdefgh\0"));
        assert_eq!("🤦‍♂️🤦‍♀️🤷‍♂️🤷‍♀️\0", convert("🤦‍♂️🤦‍♀️🤷‍♂️🤷‍♀️\0"));
        assert_eq!("\0", convert("\0abcdefgh"));
        assert_eq!("\0", convert("\0🤦‍♂️🤦‍♀️🤷‍♂️🤷‍♀️"));
        assert_eq!("abcd\0", convert("abcd\0efgh"));
        assert_eq!("🤦‍♂️🤦‍♀️\0", convert("🤦‍♂️🤦‍♀️\0🤷‍♂️🤷‍♀️"));
    }
}

#[cfg(test)]
mod ctor {
    #![allow(clippy::non_ascii_literal)]

    use crate::CToR;
    use std::{ffi::CString, os::raw::c_char, ptr};

    fn convert(string: &str) -> String {
        let string = CString::new(string).unwrap();
        unsafe { string.as_ptr().as_str() }.unwrap().to_owned()
    }

    #[test]
    fn valid() {
        assert_eq!("abcdefgh", convert("abcdefgh"));
        assert_eq!("abcd🤦‍♂️efgh", convert("abcd🤦‍♂️efgh"));
        assert_eq!("", convert(""));
        assert_eq!(None, unsafe { ptr::null::<c_char>().as_str() });
    }

    #[test]
    #[should_panic]
    fn invalid() {
        let string = CString::new(vec![0xfe, 0xfe, 0xff, 0xff]).unwrap();
        unsafe { string.as_ptr().as_str() };
    }
}

#[cfg(test)]
mod rtoc {
    #![allow(clippy::non_ascii_literal)]

    use crate::RToC;

    fn convert(string: &str) -> String {
        string
            .to_owned()
            .into_cstring()
            .to_str()
            .unwrap()
            .to_owned()
    }

    #[test]
    fn valid() {
        assert_eq!("abcdefgh", convert("abcdefgh"));
        assert_eq!("🤦‍♂️🤦‍♀️🤷‍♂️🤷‍♀️", convert("🤦‍♂️🤦‍♀️🤷‍♂️🤷‍♀️"));
        assert_eq!("abcdefgh", convert("abcdefgh\0"));
        assert_eq!("🤦‍♂️🤦‍♀️🤷‍♂️🤷‍♀️", convert("🤦‍♂️🤦‍♀️🤷‍♂️🤷‍♀️\0"));
        assert_eq!("", convert("\0abcdefgh"));
        assert_eq!("", convert("\0🤦‍♂️🤦‍♀️🤷‍♂️🤷‍♀️"));
        assert_eq!("abcd", convert("abcd\0efgh"));
        assert_eq!("🤦‍♂️🤦‍♀️", convert("🤦‍♂️🤦‍♀️\0🤷‍♂️🤷‍♀️"));
    }
}

#[cfg(test)]
#[rusty_fork::fork_test(timeout_ms = 60000)]
#[should_panic]
fn catch_panic() {
    catch(|| panic!("test"));
}