Skip to content

Commit e3b6eb1

Browse files
committed
utils: Make SealedString take a &CStr instead of a CString
It only uses it to pass it to a syscall, so it doesn’t need ownership of it.
1 parent 2eac2b2 commit e3b6eb1

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

src/input/keyboard/keymap_file.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ pub struct KeymapFile {
2121
impl KeymapFile {
2222
/// Turn the keymap into a string using KEYMAP_FORMAT_TEXT_V1, create a sealed file for it, and store the string
2323
pub fn new(keymap: &Keymap) -> Self {
24-
let name = CString::new("smithay-keymap").unwrap();
2524
let keymap = keymap.get_as_string(KEYMAP_FORMAT_TEXT_V1);
26-
let sealed = SealedFile::with_content(name, CString::new(keymap.as_str()).unwrap());
25+
let sealed = SealedFile::with_content(c"smithay-keymap", CString::new(keymap.as_str()).unwrap());
2726

2827
if let Err(err) = sealed.as_ref() {
2928
error!("Error when creating sealed keymap file: {}", err);
@@ -42,8 +41,7 @@ impl KeymapFile {
4241
pub(crate) fn change_keymap(&mut self, keymap: &Keymap) {
4342
let keymap = keymap.get_as_string(xkb::KEYMAP_FORMAT_TEXT_V1);
4443

45-
let name = CString::new("smithay-keymap-file").unwrap();
46-
let sealed = SealedFile::with_content(name, CString::new(keymap.clone()).unwrap());
44+
let sealed = SealedFile::with_content(c"smithay-keymap-file", CString::new(keymap.clone()).unwrap());
4745

4846
if let Err(err) = sealed.as_ref() {
4947
error!("Error when creating sealed keymap file: {}", err);

src/utils/sealed_file.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! information such as keymaps without them being able to write to the handle.
55
66
use std::{
7-
ffi::CString,
7+
ffi::{CStr, CString},
88
fs::File,
99
io::Write,
1010
os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd},
@@ -17,16 +17,16 @@ pub(crate) struct SealedFile {
1717
}
1818

1919
impl SealedFile {
20-
pub fn with_content(name: CString, contents: CString) -> Result<Self, std::io::Error> {
20+
pub fn with_content(name: &CStr, contents: CString) -> Result<Self, std::io::Error> {
2121
Self::with_data(name, contents.as_bytes_with_nul())
2222
}
2323

2424
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "android"))]
25-
pub fn with_data(name: CString, data: &[u8]) -> Result<Self, std::io::Error> {
25+
pub fn with_data(name: &CStr, data: &[u8]) -> Result<Self, std::io::Error> {
2626
use rustix::fs::{MemfdFlags, SealFlags};
2727
use std::io::Seek;
2828

29-
let fd = rustix::fs::memfd_create(&name, MemfdFlags::CLOEXEC | MemfdFlags::ALLOW_SEALING)?;
29+
let fd = rustix::fs::memfd_create(name, MemfdFlags::CLOEXEC | MemfdFlags::ALLOW_SEALING)?;
3030

3131
let mut file: File = fd.into();
3232
file.write_all(data)?;
@@ -46,7 +46,7 @@ impl SealedFile {
4646
}
4747

4848
#[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "android")))]
49-
pub fn with_data(name: CString, data: &[u8]) -> Result<Self, std::io::Error> {
49+
pub fn with_data(name: &CStr, data: &[u8]) -> Result<Self, std::io::Error> {
5050
use rand::{distributions::Alphanumeric, Rng};
5151
use rustix::{
5252
io::Errno,
@@ -59,7 +59,7 @@ impl SealedFile {
5959
// loop a couple times if it exists.
6060
let mut n = 0;
6161
let (shm_name, mut file) = loop {
62-
let mut shm_name = name.as_bytes().to_owned();
62+
let mut shm_name = name.to_bytes().to_owned();
6363
shm_name.push(b'-');
6464
shm_name.extend((0..7).map(|_| rng.sample(Alphanumeric)));
6565
let fd = rustix::shm::shm_open(

src/wayland/dmabuf/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ mod dispatch;
189189

190190
use std::{
191191
collections::HashMap,
192-
ffi::CString,
193192
ops::Sub,
194193
os::unix::io::AsFd,
195194
sync::{
@@ -397,8 +396,7 @@ impl DmabufFeedbackBuilder {
397396
.flat_map(DmabufFeedbackFormat::to_ne_bytes)
398397
.collect::<Vec<_>>();
399398

400-
let name = CString::new("smithay-dmabuffeedback-format-table").unwrap();
401-
let format_table_file = SealedFile::with_data(name, &formats)?;
399+
let format_table_file = SealedFile::with_data(c"smithay-dmabuffeedback-format-table", &formats)?;
402400

403401
// remove all formats from the main tranche that are already covered
404402
// by a preference tranche

0 commit comments

Comments
 (0)