Skip to content

Commit

Permalink
utils: Make SealedString take a &CStr instead of a CString
Browse files Browse the repository at this point in the history
It only uses it to pass it to a syscall, so it doesn’t need ownership of
it.
  • Loading branch information
linkmauve committed Jun 30, 2024
1 parent 253e13e commit dfb4e78
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
6 changes: 2 additions & 4 deletions src/input/keyboard/keymap_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ pub struct KeymapFile {
impl KeymapFile {
/// Turn the keymap into a string using KEYMAP_FORMAT_TEXT_V1, create a sealed file for it, and store the string
pub fn new(keymap: &Keymap) -> Self {
let name = CString::new("smithay-keymap").unwrap();
let keymap = keymap.get_as_string(KEYMAP_FORMAT_TEXT_V1);
let sealed = SealedFile::with_content(name, CString::new(keymap.as_str()).unwrap());
let sealed = SealedFile::with_content(c"smithay-keymap", CString::new(keymap.as_str()).unwrap());

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

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

if let Err(err) = sealed.as_ref() {
error!("Error when creating sealed keymap file: {}", err);
Expand Down
12 changes: 6 additions & 6 deletions src/utils/sealed_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! information such as keymaps without them being able to write to the handle.
use std::{
ffi::CString,
ffi::{CStr, CString},
fs::File,
io::Write,
os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd},
Expand All @@ -17,16 +17,16 @@ pub(crate) struct SealedFile {
}

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

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

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

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

#[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "android")))]
pub fn with_data(name: CString, data: &[u8]) -> Result<Self, std::io::Error> {
pub fn with_data(name: &CStr, data: &[u8]) -> Result<Self, std::io::Error> {
use rand::{distributions::Alphanumeric, Rng};
use rustix::{
io::Errno,
Expand All @@ -59,7 +59,7 @@ impl SealedFile {
// loop a couple times if it exists.
let mut n = 0;
let (shm_name, mut file) = loop {
let mut shm_name = name.as_bytes().to_owned();
let mut shm_name = name.to_bytes().to_owned();
shm_name.push(b'-');
shm_name.extend((0..7).map(|_| rng.sample(Alphanumeric)));
let fd = rustix::shm::shm_open(
Expand Down
4 changes: 1 addition & 3 deletions src/wayland/dmabuf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ mod dispatch;

use std::{
collections::HashMap,
ffi::CString,
ops::Sub,
os::unix::io::AsFd,
sync::{
Expand Down Expand Up @@ -397,8 +396,7 @@ impl DmabufFeedbackBuilder {
.flat_map(DmabufFeedbackFormat::to_ne_bytes)
.collect::<Vec<_>>();

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

// remove all formats from the main tranche that are already covered
// by a preference tranche
Expand Down

0 comments on commit dfb4e78

Please sign in to comment.