Skip to content

Commit 7066467

Browse files
authored
RUST-1952 Use std::ffi::c_char instead of *const i8 (#30)
1 parent ee15682 commit 7066467

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

mongocrypt/src/convert.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
use std::ffi::CString;
2+
13
use bson::{Document, RawDocument};
24
use mongocrypt_sys as sys;
35

46
use crate::{
57
binary::BinaryBuf,
6-
error::{self, Result},
8+
error::{self, encoding, Result},
79
};
810

911
pub(crate) fn doc_binary(doc: &Document) -> Result<BinaryBuf> {
@@ -13,15 +15,20 @@ pub(crate) fn doc_binary(doc: &Document) -> Result<BinaryBuf> {
1315
Ok(BinaryBuf::new(bytes))
1416
}
1517

18+
pub(crate) fn path_cstring(path: &std::path::Path) -> Result<CString> {
19+
let bytes = path_bytes(path)?;
20+
CString::new(bytes).map_err(|e| encoding!("could not convert path to cstring: {:?}", e))
21+
}
22+
1623
#[cfg(unix)]
17-
pub(crate) fn path_bytes(path: &std::path::Path) -> Result<Vec<u8>> {
24+
fn path_bytes(path: &std::path::Path) -> Result<Vec<u8>> {
1825
use std::os::unix::prelude::OsStrExt;
1926

2027
Ok(path.as_os_str().as_bytes().to_vec())
2128
}
2229

2330
#[cfg(not(unix))]
24-
pub(crate) fn path_bytes(path: &std::path::Path) -> Result<Vec<u8>> {
31+
fn path_bytes(path: &std::path::Path) -> Result<Vec<u8>> {
2532
// This is correct for Windows because libmongocrypt internally converts
2633
// from utf8 to utf16 on that platform.
2734
use error::Error;
@@ -32,9 +39,9 @@ pub(crate) fn path_bytes(path: &std::path::Path) -> Result<Vec<u8>> {
3239
Ok(s.as_bytes().to_vec())
3340
}
3441

35-
pub(crate) fn str_bytes_len(s: &str) -> Result<(*const i8, i32)> {
42+
pub(crate) fn str_bytes_len(s: &str) -> Result<(*const std::ffi::c_char, i32)> {
3643
Ok((
37-
s.as_bytes().as_ptr() as *const i8,
44+
s.as_bytes().as_ptr() as *const std::ffi::c_char,
3845
s.as_bytes().len().try_into()?,
3946
))
4047
}

mongocrypt/src/hooks.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ fn write_status(result: Result<()>, c_status: *mut sys::mongocrypt_status_t) ->
360360
c_status,
361361
sys::mongocrypt_status_type_t_MONGOCRYPT_STATUS_ERROR_CLIENT,
362362
0,
363-
b"Failed to record error, see logs for details\0".as_ptr() as *const i8,
363+
b"Failed to record error, see logs for details\0".as_ptr()
364+
as *const std::ffi::c_char,
364365
-1,
365366
);
366367
}

mongocrypt/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{borrow::Borrow, ffi::CStr, path::Path, ptr, sync::Mutex};
33
use bson::Document;
44
#[cfg(test)]
55
use convert::str_bytes_len;
6-
use convert::{doc_binary, path_bytes};
6+
use convert::{doc_binary, path_cstring};
77
use ctx::CtxBuilder;
88
use mongocrypt_sys as sys;
99

@@ -155,12 +155,11 @@ impl CryptBuilder {
155155
/// `set_crypt_shared_lib_path_override`, then paths
156156
/// appended here will have no effect.
157157
pub fn append_crypt_shared_lib_search_path(self, path: &Path) -> Result<Self> {
158-
let mut tmp = path_bytes(path)?;
159-
tmp.push(0);
158+
let tmp = path_cstring(path)?;
160159
unsafe {
161160
sys::mongocrypt_setopt_append_crypt_shared_lib_search_path(
162161
*self.inner.borrow(),
163-
tmp.as_ptr() as *const i8,
162+
tmp.as_ptr(),
164163
);
165164
}
166165
Ok(self)
@@ -184,12 +183,11 @@ impl CryptBuilder {
184183
/// initialize a valid crypt_shared library instance for the path specified, then
185184
/// the initialization will fail with an error.
186185
pub fn set_crypt_shared_lib_path_override(self, path: &Path) -> Result<Self> {
187-
let mut tmp = path_bytes(path)?;
188-
tmp.push(0);
186+
let tmp = path_cstring(path)?;
189187
unsafe {
190188
sys::mongocrypt_setopt_set_crypt_shared_lib_path_override(
191189
*self.inner.borrow(),
192-
tmp.as_ptr() as *const i8,
190+
tmp.as_ptr(),
193191
);
194192
}
195193
Ok(self)

0 commit comments

Comments
 (0)