diff --git a/CHANGELOG.md b/CHANGELOG.md index 39703ce..de214a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 24714f2..0f30411 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libftd2xx" -version = "0.33.0" +version = "0.33.1" authors = ["Alex Martens "] edition = "2021" description = "Rust safe wrapper around the libftd2xx-ffi crate." diff --git a/README.md b/README.md index f8e5560..614afeb 100644 --- a/README.md +++ b/README.md @@ -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"] diff --git a/src/lib.rs b/src/lib.rs index 837e282..4da51f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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"] @@ -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 { 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) }; @@ -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"); + } +}