Commit a2356ab
Merge #1921
1921: feat: I/O safety for 'sys/termios' & 'pty' r=asomers a=SteveLauC
#### What this PR does:
1. Adds I/O safety for modules `sys/termios` and `pty`
------
#### Known Problems:
1. [Double free issue on `PtyMaster`](#659)
I have changed the `RawFd` in `PtyMaster` to `OwnedFd` in this PR, with this
change, the double-free issue still exists, see this test code snippet
(From [this comment](#659 (comment)))
```rust
use std::io::prelude::*;
use std::os::unix::io::AsRawFd;
fn main() {
let mut f = {
let m = nix::pty::posix_openpt(nix::fcntl::OFlag::O_RDWR).unwrap(); // get fd 3
nix::unistd::close(m.as_raw_fd()).unwrap(); // close fd 3
std::fs::File::create("foo").unwrap() // get fd 3 again
}; // m goes out of scope, `drop(OwnedFd)`, fd 3 closed
f.write("whatever".as_bytes()).unwrap(); // EBADF
}
```
I have tested this code with `nix 0.26.1`, and I am still getting `EBADF`, which means the current impl does not prevent this problem either.
```shell
$ cat Cargo.toml | grep nix
nix = "0.26.1"
$ cargo r -q
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 9, kind: Uncategorized, message: "Bad file descriptor" }', src/main.rs:10:36
```
If we still wanna the drop of `PtyMaster` panic when the internal `fd` is invalid
as we did in #677, then we have to revert the changes to use `RawFd` and manually impl `Drop`.
2. Some trait implementations for some types are removed
* `struct OpenptyResult`:
1. PartialEq
2. Eq
3. Hash
4. Clone
* `struct ForkptyResult`:
1. Clone
* `struct PtyMaster`:
1. PartialEq
2. Eq
3. Hash
In the previous implementation, these trait impls are `#[derive()]`ed, due to
the type change to `OwnedFd`, we can no longer derive them. Should we manually
implement them?
I kinda think we should at least impl `PartialEq` and `Eq` for `OpenptyResult`
and `PtyMaster`.
-----
#### Some Clarifications that may help code review
1. For the basic `fd`-related syscall like `read(2)`, `write(2)` and `fcntl(2)`
, I am still using the old `RawFd` interfaces, as they will be covered in
other PRs.
2. Two helper functions
1. `write_all()` in `test/sys/test_termios.rs`:
```rust
/// Helper function analogous to `std::io::Write::write_all`, but for `RawFd`s
fn write_all(f: RawFd, buf: &[u8]) {
/// Helper function analogous to `std::io::Write::write_all`, but for `Fd`s
fn write_all<Fd: AsFd>(f: &Fd, buf: &[u8]) {
let mut len = 0;
while len < buf.len() {
len += write(f, &buf[len..]).unwrap();
len += write(f.as_fd().as_raw_fd(), &buf[len..]).unwrap();
}
}
```
2. `read_exact()` in `test/test.rs`:
```rust
/// Helper function analogous to `std::io::Read::read_exact`, but for `RawFD`s
fn read_exact(f: RawFd, buf: &mut [u8]) {
/// Helper function analogous to `std::io::Read::read_exact`, but for `Fd`s
fn read_exact<Fd: AsFd>(f: &Fd, buf: &mut [u8]) {
let mut len = 0;
while len < buf.len() {
// get_mut would be better than split_at_mut, but it requires nightly
let (_, remaining) = buf.split_at_mut(len);
len += read(f, remaining).unwrap();
len += read(f.as_fd().as_raw_fd(), remaining).unwrap();
}
}
```
I have added I/O safety for them, but it actually does not matter whether
they use `Fd: AsFd` or `RawFd`. So feel free to ask me to discard these changes
if you guys don't like it.
Co-authored-by: Steve Lau <stevelauc@outlook.com>File tree
9 files changed
+99
-193
lines changed- src
- sys
- test
- sys
9 files changed
+99
-193
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
19 | 22 | | |
20 | 23 | | |
21 | 24 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | | - | |
21 | | - | |
| 19 | + | |
| 20 | + | |
22 | 21 | | |
23 | 22 | | |
24 | | - | |
| 23 | + | |
25 | 24 | | |
26 | | - | |
| 25 | + | |
27 | 26 | | |
28 | 27 | | |
29 | 28 | | |
30 | 29 | | |
31 | 30 | | |
32 | 31 | | |
33 | | - | |
34 | | - | |
35 | | - | |
| 32 | + | |
| 33 | + | |
36 | 34 | | |
37 | 35 | | |
38 | | - | |
| 36 | + | |
39 | 37 | | |
40 | 38 | | |
41 | 39 | | |
42 | 40 | | |
43 | 41 | | |
44 | 42 | | |
45 | 43 | | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
51 | 48 | | |
52 | 49 | | |
53 | 50 | | |
54 | | - | |
| 51 | + | |
55 | 52 | | |
56 | 53 | | |
57 | 54 | | |
58 | 55 | | |
59 | 56 | | |
60 | 57 | | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
| 58 | + | |
79 | 59 | | |
80 | 60 | | |
81 | 61 | | |
82 | 62 | | |
83 | 63 | | |
84 | | - | |
| 64 | + | |
85 | 65 | | |
86 | 66 | | |
87 | 67 | | |
88 | 68 | | |
89 | 69 | | |
90 | | - | |
| 70 | + | |
91 | 71 | | |
92 | 72 | | |
93 | 73 | | |
| |||
96 | 76 | | |
97 | 77 | | |
98 | 78 | | |
99 | | - | |
| 79 | + | |
100 | 80 | | |
101 | 81 | | |
102 | 82 | | |
103 | 83 | | |
104 | 84 | | |
105 | | - | |
| 85 | + | |
106 | 86 | | |
107 | 87 | | |
108 | 88 | | |
| |||
164 | 144 | | |
165 | 145 | | |
166 | 146 | | |
167 | | - | |
| 147 | + | |
168 | 148 | | |
169 | 149 | | |
170 | 150 | | |
| |||
308 | 288 | | |
309 | 289 | | |
310 | 290 | | |
311 | | - | |
312 | | - | |
| 291 | + | |
| 292 | + | |
313 | 293 | | |
314 | 294 | | |
315 | 295 | | |
| |||
364 | 344 | | |
365 | 345 | | |
366 | 346 | | |
367 | | - | |
| 347 | + | |
368 | 348 | | |
369 | 349 | | |
370 | 350 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
222 | 222 | | |
223 | 223 | | |
224 | 224 | | |
225 | | - | |
| 225 | + | |
226 | 226 | | |
227 | 227 | | |
228 | 228 | | |
| |||
1143 | 1143 | | |
1144 | 1144 | | |
1145 | 1145 | | |
1146 | | - | |
| 1146 | + | |
1147 | 1147 | | |
1148 | 1148 | | |
1149 | | - | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
1150 | 1152 | | |
1151 | 1153 | | |
1152 | 1154 | | |
| |||
1159 | 1161 | | |
1160 | 1162 | | |
1161 | 1163 | | |
1162 | | - | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
1163 | 1169 | | |
1164 | 1170 | | |
1165 | | - | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
1166 | 1176 | | |
1167 | 1177 | | |
1168 | 1178 | | |
1169 | 1179 | | |
1170 | 1180 | | |
1171 | 1181 | | |
1172 | | - | |
1173 | | - | |
| 1182 | + | |
| 1183 | + | |
1174 | 1184 | | |
1175 | 1185 | | |
1176 | 1186 | | |
1177 | 1187 | | |
1178 | 1188 | | |
1179 | 1189 | | |
1180 | 1190 | | |
1181 | | - | |
1182 | | - | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + | |
1183 | 1196 | | |
1184 | 1197 | | |
1185 | 1198 | | |
1186 | 1199 | | |
1187 | 1200 | | |
1188 | 1201 | | |
1189 | 1202 | | |
1190 | | - | |
1191 | | - | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
1192 | 1208 | | |
1193 | 1209 | | |
1194 | 1210 | | |
1195 | 1211 | | |
1196 | 1212 | | |
1197 | 1213 | | |
1198 | 1214 | | |
1199 | | - | |
1200 | | - | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
1201 | 1220 | | |
1202 | 1221 | | |
1203 | 1222 | | |
1204 | 1223 | | |
1205 | 1224 | | |
1206 | 1225 | | |
1207 | | - | |
1208 | | - | |
| 1226 | + | |
| 1227 | + | |
1209 | 1228 | | |
1210 | 1229 | | |
1211 | 1230 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | | - | |
11 | | - | |
| 10 | + | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
26 | | - | |
27 | | - | |
| 25 | + | |
28 | 26 | | |
29 | 27 | | |
30 | 28 | | |
31 | 29 | | |
32 | 30 | | |
33 | 31 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
| 32 | + | |
44 | 33 | | |
45 | 34 | | |
46 | 35 | | |
| |||
52 | 41 | | |
53 | 42 | | |
54 | 43 | | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
| 44 | + | |
61 | 45 | | |
62 | 46 | | |
63 | 47 | | |
| |||
73 | 57 | | |
74 | 58 | | |
75 | 59 | | |
76 | | - | |
77 | | - | |
78 | 60 | | |
79 | 61 | | |
80 | 62 | | |
81 | | - | |
| 63 | + | |
82 | 64 | | |
83 | 65 | | |
84 | 66 | | |
85 | | - | |
| 67 | + | |
86 | 68 | | |
87 | | - | |
88 | | - | |
89 | 69 | | |
90 | 70 | | |
91 | 71 | | |
| |||
98 | 78 | | |
99 | 79 | | |
100 | 80 | | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
| 81 | + | |
107 | 82 | | |
108 | 83 | | |
109 | 84 | | |
| |||
114 | 89 | | |
115 | 90 | | |
116 | 91 | | |
117 | | - | |
118 | | - | |
119 | 92 | | |
120 | 93 | | |
121 | | - | |
| 94 | + | |
122 | 95 | | |
123 | 96 | | |
124 | | - | |
| 97 | + | |
125 | 98 | | |
126 | 99 | | |
127 | 100 | | |
128 | | - | |
| 101 | + | |
129 | 102 | | |
130 | 103 | | |
131 | 104 | | |
132 | | - | |
133 | | - | |
134 | | - | |
| 105 | + | |
135 | 106 | | |
136 | 107 | | |
0 commit comments