Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
fix: invalid memory accesses
Browse files Browse the repository at this point in the history
  • Loading branch information
llenotre committed Jun 10, 2024
1 parent 315e6d5 commit 585c440
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::ffi::c_int;
use std::ffi::c_ulong;
use std::ffi::c_void;
use std::ffi::CStr;
use std::ffi::CString;
use std::os::unix::ffi::OsStrExt;
use std::path::Path;
use std::process::{Command, Stdio};
Expand Down Expand Up @@ -52,8 +53,8 @@ macro_rules! test_assert_eq {
}

pub fn chmod<P: AsRef<Path>>(path: P, mode: mode_t) -> io::Result<()> {
let path = path.as_ref().as_os_str().as_bytes().as_ptr() as _;
let res = unsafe { libc::chmod(path, mode) };
let path = CString::new(path.as_ref().as_os_str().as_bytes())?;
let res = unsafe { libc::chmod(path.as_ptr(), mode) };
if res >= 0 {
Ok(())
} else {
Expand All @@ -71,10 +72,10 @@ pub fn fchmod(fd: c_int, mode: mode_t) -> io::Result<()> {
}

pub fn stat<P: AsRef<Path>>(path: P) -> io::Result<libc::stat> {
let path = CString::new(path.as_ref().as_os_str().as_bytes())?;
unsafe {
let mut stat: libc::stat = mem::zeroed();
let path = path.as_ref().as_os_str().as_bytes().as_ptr() as _;
let res = libc::stat(path, &mut stat);
let res = libc::stat(path.as_ptr(), &mut stat);
if res >= 0 {
Ok(stat)
} else {
Expand Down

0 comments on commit 585c440

Please sign in to comment.