FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

ffi: No interior NULs (part 1) by joshuamegnauth54 · Pull Request #8245 · RustPython/RustPython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .rs  (20) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
18 changes: 11 additions & 7 deletions crates/host_env/src/ctypes.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ use libloading::Library;
use libloading::os::unix::Library as UnixLibrary;
#[cfg(any(unix, windows))]
use parking_lot::{Mutex, RwLock};
use rustpython_wtf8::Wtf8;
use rustpython_wtf8::Wtf8Buf;
use rustpython_wtf8::{Wtf8, Wtf8Buf};
#[cfg(any(unix, windows))]
use std::{collections::HashMap, ffi::OsStr, sync::OnceLock};
use widestring::WideCStr;
Expand Down Expand Up @@ -503,7 +502,7 @@ pub fn encode_wtf8_to_wchar_padded(s: &Wtf8, size: usize) -> Vec<u8> {
wchar_bytes
}

pub fn wchar_null_terminated_bytes(s: &Wtf8) -> Vec<u8> {
pub fn clone_wchar_null_terminated(s: &Wtf8) -> Vec<u8> {
if size_of::<WChar>() == 2 {
// We can't cast u32 to WChar because it would truncate the value on platforms where WChar
// is two bytes. Wtf8::encode_wide does all of the hard work for us, so all we have to do
Expand Down Expand Up @@ -1091,10 +1090,15 @@ pub fn utf16z_bytes(s: &Wtf8) -> Vec<u8> {
.collect()
}

pub fn null_terminated_bytes(bytes: &[u8]) -> Vec<u8> {
let mut buffer = bytes.to_vec();
buffer.push(0);
buffer
/// Return a NUL terminated copy of `bytes`.
///
/// The input may contain interior NULs.
pub fn clone_as_null_terminated(bytes: &[u8]) -> Vec<u8> {
if bytes.last() == Some(&0) {
bytes.to_vec()
} else {
bytes.iter().copied().chain(Some(0)).collect()
}
}

pub fn decode_type_code(type_code: &str, bytes: &[u8]) -> DecodedValue {
Expand Down
29 changes: 11 additions & 18 deletions crates/host_env/src/fileutils.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ pub fn fstat(fd: crate::crt_fd::Borrowed<'_>) -> std::io::Result<StatStruct> {
#[cfg(windows)]
pub mod windows {
use crate::crt_fd;
use crate::windows::ToWideString;
use libc::{S_IFCHR, S_IFDIR, S_IFMT};
use std::ffi::OsStr;
use std::os::windows::io::AsRawHandle;
use std::path::Path;
use std::sync::OnceLock;
use windows_sys::Win32::Foundation::{
ERROR_INVALID_HANDLE, ERROR_NOT_SUPPORTED, FILETIME, FreeLibrary, SetLastError,
Expand Down Expand Up @@ -67,21 +67,15 @@ pub mod windows {
impl StatStruct {
// update_st_mode_from_path in cpython
pub fn update_st_mode_from_path(&mut self, path: &OsStr, attr: u32) {
if attr & FILE_ATTRIBUTE_DIRECTORY == 0 {
let file_extension = path
.to_wide()
.split(|&c| c == '.' as u16)
.next_back()
.and_then(|s| String::from_utf16(s).ok());

if let Some(file_extension) = file_extension
&& (file_extension.eq_ignore_ascii_case("exe")
|| file_extension.eq_ignore_ascii_case("bat")
|| file_extension.eq_ignore_ascii_case("cmd")
|| file_extension.eq_ignore_ascii_case("com"))
{
self.st_mode |= 0o111;
}
if attr & FILE_ATTRIBUTE_DIRECTORY == 0
&& let Some(file_extension) =
Path::new(path).extension().and_then(|ext| ext.to_str())
&& (file_extension.eq_ignore_ascii_case("exe")
|| file_extension.eq_ignore_ascii_case("bat")
|| file_extension.eq_ignore_ascii_case("cmd")
|| file_extension.eq_ignore_ascii_case("com"))
{
self.st_mode |= 0o111;
}
}
}
Expand Down Expand Up @@ -288,7 +282,7 @@ pub mod windows {

// _Py_GetFileInformationByName in cpython
pub fn get_file_information_by_name(
file_name: &OsStr,
file_name: &widestring::WideCStr,
file_information_class: FILE_INFO_BY_NAME_CLASS,
) -> std::io::Result<FILE_STAT_BASIC_INFORMATION> {
static GET_FILE_INFORMATION_BY_NAME: OnceLock<
Expand Down Expand Up @@ -329,7 +323,6 @@ pub mod windows {
})
.ok_or_else(|| std::io::Error::from_raw_os_error(ERROR_NOT_SUPPORTED as _))?;

let file_name = file_name.to_wide_with_nul();
let file_info_buffer_size = core::mem::size_of::<FILE_STAT_BASIC_INFORMATION>() as u32;
let mut file_info_buffer = core::mem::MaybeUninit::<FILE_STAT_BASIC_INFORMATION>::uninit();
unsafe {
Expand Down
Loading
Loading

Back | FazBrowse Home | New Git URL