Skip to content

Commit 67e8ccd

Browse files
committed
console: avoid using the raw ioctl for TIOCGWINSZ
Using the raw ioctl can cause UB that manifest on some versions of the compiler. Use nix's ioctl_read_bad to generate a proper ioctl wrapper. Signed-off-by: Sergio Lopez <[email protected]>
1 parent 8fa92dd commit 67e8ccd

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/devices/src/virtio/console/device.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::sync::atomic::AtomicUsize;
77
use std::sync::{Arc, Mutex};
88

99
use libc::TIOCGWINSZ;
10+
use nix::ioctl_read_bad;
1011
use utils::eventfd::EventFd;
1112
use vm_memory::{ByteValued, Bytes, GuestMemoryMmap};
1213

@@ -33,22 +34,27 @@ pub(crate) const AVAIL_FEATURES: u64 = 1 << uapi::VIRTIO_CONSOLE_F_SIZE as u64
3334
| 1 << uapi::VIRTIO_CONSOLE_F_MULTIPORT as u64
3435
| 1 << uapi::VIRTIO_F_VERSION_1 as u64;
3536

37+
#[repr(C)]
38+
#[derive(Default)]
39+
struct WS {
40+
rows: u16,
41+
cols: u16,
42+
xpixel: u16,
43+
ypixel: u16,
44+
}
45+
ioctl_read_bad!(tiocgwinsz, TIOCGWINSZ, WS);
46+
3647
pub(crate) fn get_win_size() -> (u16, u16) {
37-
#[repr(C)]
38-
#[derive(Default)]
39-
struct WS {
40-
rows: u16,
41-
cols: u16,
42-
xpixel: u16,
43-
ypixel: u16,
44-
}
45-
let ws: WS = WS::default();
48+
let mut ws: WS = WS::default();
4649

47-
unsafe {
48-
libc::ioctl(0, TIOCGWINSZ, &ws);
49-
}
50+
let ret = unsafe { tiocgwinsz(0, &mut ws) };
5051

51-
(ws.cols, ws.rows)
52+
if let Err(err) = ret {
53+
error!("Couldn't get terminal dimensions: {}", err);
54+
(0, 0)
55+
} else {
56+
(ws.cols, ws.rows)
57+
}
5258
}
5359

5460
#[derive(Copy, Clone, Debug, Default)]

0 commit comments

Comments
 (0)