Skip to content

Commit 8a6046a

Browse files
authored
Fix panic on &str to CString conversion
1 parent b4a4c1b commit 8a6046a

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.33.1] - 2024-11-16
8+
### Fixed
9+
- Fixed a panic when opening a device with strings containing null bytes.
10+
711
## [0.33.0] - 2024-09-05
812
### Added
913
- Added FT240X EEPROM.
@@ -167,7 +171,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
167171
## Prior releases
168172
A changelog was not kept for prior releases.
169173

170-
[Unreleased]: https://github.com/ftdi-rs/libftd2xx/compare/0.33.0...HEAD
174+
[Unreleased]: https://github.com/ftdi-rs/libftd2xx/compare/0.33.1...HEAD
175+
[0.33.1]: https://github.com/ftdi-rs/libftd2xx/compare/0.33.0...0.33.1
171176
[0.33.0]: https://github.com/ftdi-rs/libftd2xx/compare/0.32.5...0.33.0
172177
[0.32.5]: https://github.com/ftdi-rs/libftd2xx/compare/0.32.4...0.32.5
173178
[0.32.4]: https://github.com/ftdi-rs/libftd2xx/compare/0.32.3...0.32.4

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "libftd2xx"
3-
version = "0.33.0"
3+
version = "0.33.1"
44
authors = ["Alex Martens <alex@thinglab.org>"]
55
edition = "2021"
66
description = "Rust safe wrapper around the libftd2xx-ffi crate."

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Simply add this crate as a dependency in your `Cargo.toml`.
1414

1515
```toml
1616
[dependencies.libftd2xx]
17-
version = "0.33.0"
17+
version = "0.33.1"
1818
# statically link the vendor library, defaults to dynamic if not set
1919
# this will make things "just work" on Linux and Windows
2020
features = ["static"]

src/lib.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//!
99
//! ```toml
1010
//! [dependencies.libftd2xx]
11-
//! version = "0.33.0"
11+
//! version = "0.33.1"
1212
//! # statically link the vendor library, defaults to dynamic if not set
1313
//! # this will make things "just work" on Linux and Windows
1414
//! features = ["static"]
@@ -2003,9 +2003,13 @@ pub trait FtdiEeprom<
20032003
}
20042004
}
20052005

2006+
fn str_to_cstring(s: &str) -> std::ffi::CString {
2007+
std::ffi::CString::new(s).unwrap_or(std::ffi::CString::from(c""))
2008+
}
2009+
20062010
fn ft_open_ex(arg: &str, flag: u32) -> Result<FT_HANDLE, FtStatus> {
20072011
let mut handle: FT_HANDLE = std::ptr::null_mut();
2008-
let cstr_arg = std::ffi::CString::new(arg).unwrap();
2012+
let cstr_arg = str_to_cstring(arg);
20092013
trace!(r#"FT_OpenEx("{}", {}, _)"#, arg, flag);
20102014
let status: FT_STATUS =
20112015
unsafe { FT_OpenEx(cstr_arg.as_ptr() as *mut c_void, flag, &mut handle) };
@@ -2473,3 +2477,22 @@ impl Ftx232hMpsse for Ft232h {}
24732477
impl Ftx232hMpsse for Ft2232h {}
24742478
impl Ftx232hMpsse for Ft4232h {}
24752479
impl Ftx232hMpsse for Ft4232ha {}
2480+
2481+
#[cfg(test)]
2482+
mod tests {
2483+
use super::str_to_cstring;
2484+
2485+
#[test]
2486+
fn str_to_cstr_basic() {
2487+
assert_eq!(
2488+
str_to_cstring("Hello, World!"),
2489+
std::ffi::CString::from(c"Hello, World!")
2490+
);
2491+
}
2492+
2493+
// https://github.com/ftdi-rs/libftd2xx/issues/83
2494+
#[test]
2495+
fn str_to_cstr_interior_null() {
2496+
str_to_cstring("\0\u{e}.r");
2497+
}
2498+
}

0 commit comments

Comments
 (0)