Skip to content

Commit

Permalink
Fix panic on &str to CString conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
newAM authored Nov 16, 2024
1 parent b4a4c1b commit 8a6046a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.33.1] - 2024-11-16
### Fixed
- Fixed a panic when opening a device with strings containing null bytes.

## [0.33.0] - 2024-09-05
### Added
- Added FT240X EEPROM.
Expand Down Expand Up @@ -167,7 +171,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Prior releases
A changelog was not kept for prior releases.

[Unreleased]: https://github.com/ftdi-rs/libftd2xx/compare/0.33.0...HEAD
[Unreleased]: https://github.com/ftdi-rs/libftd2xx/compare/0.33.1...HEAD
[0.33.1]: https://github.com/ftdi-rs/libftd2xx/compare/0.33.0...0.33.1
[0.33.0]: https://github.com/ftdi-rs/libftd2xx/compare/0.32.5...0.33.0
[0.32.5]: https://github.com/ftdi-rs/libftd2xx/compare/0.32.4...0.32.5
[0.32.4]: https://github.com/ftdi-rs/libftd2xx/compare/0.32.3...0.32.4
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libftd2xx"
version = "0.33.0"
version = "0.33.1"
authors = ["Alex Martens <[email protected]>"]
edition = "2021"
description = "Rust safe wrapper around the libftd2xx-ffi crate."
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Simply add this crate as a dependency in your `Cargo.toml`.

```toml
[dependencies.libftd2xx]
version = "0.33.0"
version = "0.33.1"
# statically link the vendor library, defaults to dynamic if not set
# this will make things "just work" on Linux and Windows
features = ["static"]
Expand Down
27 changes: 25 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//!
//! ```toml
//! [dependencies.libftd2xx]
//! version = "0.33.0"
//! version = "0.33.1"
//! # statically link the vendor library, defaults to dynamic if not set
//! # this will make things "just work" on Linux and Windows
//! features = ["static"]
Expand Down Expand Up @@ -2003,9 +2003,13 @@ pub trait FtdiEeprom<
}
}

fn str_to_cstring(s: &str) -> std::ffi::CString {
std::ffi::CString::new(s).unwrap_or(std::ffi::CString::from(c""))
}

fn ft_open_ex(arg: &str, flag: u32) -> Result<FT_HANDLE, FtStatus> {
let mut handle: FT_HANDLE = std::ptr::null_mut();
let cstr_arg = std::ffi::CString::new(arg).unwrap();
let cstr_arg = str_to_cstring(arg);
trace!(r#"FT_OpenEx("{}", {}, _)"#, arg, flag);
let status: FT_STATUS =
unsafe { FT_OpenEx(cstr_arg.as_ptr() as *mut c_void, flag, &mut handle) };
Expand Down Expand Up @@ -2473,3 +2477,22 @@ impl Ftx232hMpsse for Ft232h {}
impl Ftx232hMpsse for Ft2232h {}
impl Ftx232hMpsse for Ft4232h {}
impl Ftx232hMpsse for Ft4232ha {}

#[cfg(test)]
mod tests {
use super::str_to_cstring;

#[test]
fn str_to_cstr_basic() {
assert_eq!(
str_to_cstring("Hello, World!"),
std::ffi::CString::from(c"Hello, World!")
);
}

// https://github.com/ftdi-rs/libftd2xx/issues/83
#[test]
fn str_to_cstr_interior_null() {
str_to_cstring("\0\u{e}.r");
}
}

0 comments on commit 8a6046a

Please sign in to comment.