Use CStr::to_str where possible

This commit is contained in:
gwenn 2024-03-10 11:53:29 +01:00
parent 6036587952
commit 87e324d720
7 changed files with 19 additions and 25 deletions

View File

@ -128,10 +128,9 @@ impl InnerConnection {
let callback: fn(&Connection, &str) -> Result<()> = mem::transmute(arg1);
let res = catch_unwind(|| {
let conn = Connection::from_handle(arg2).unwrap();
let collation_name = {
let c_slice = CStr::from_ptr(arg3).to_bytes();
str::from_utf8(c_slice).expect("illegal collation sequence name")
};
let collation_name = CStr::from_ptr(arg3)
.to_str()
.expect("illegal collation sequence name");
callback(&conn, collation_name)
});
if res.is_err() {

View File

@ -104,7 +104,9 @@ impl Statement<'_> {
// clippy::or_fun_call (nightly) vs clippy::unnecessary-lazy-evaluations (stable)
.ok_or(Error::InvalidColumnIndex(col))
.map(|slice| {
str::from_utf8(slice.to_bytes()).expect("Invalid UTF-8 sequence in column name")
slice
.to_str()
.expect("Invalid UTF-8 sequence in column name")
})
}
@ -149,7 +151,8 @@ impl Statement<'_> {
let name = self.column_name_unwrap(i);
let slice = self.stmt.column_decltype(i);
let decl_type = slice.map(|s| {
str::from_utf8(s.to_bytes()).expect("Invalid UTF-8 sequence in column declaration")
s.to_str()
.expect("Invalid UTF-8 sequence in column declaration")
});
cols.push(Column { name, decl_type });
}

View File

@ -666,7 +666,8 @@ unsafe fn expect_optional_utf8<'a>(
if p_str.is_null() {
return None;
}
std::str::from_utf8(std::ffi::CStr::from_ptr(p_str).to_bytes())
std::ffi::CStr::from_ptr(p_str)
.to_str()
.unwrap_or_else(|_| panic!("received non-utf8 string as {description}"))
.into()
}

View File

@ -289,8 +289,7 @@ impl<T> OptionalExtension<T> for Result<T> {
}
unsafe fn errmsg_to_string(errmsg: *const c_char) -> String {
let c_slice = CStr::from_ptr(errmsg).to_bytes();
String::from_utf8_lossy(c_slice).into_owned()
CStr::from_ptr(errmsg).to_string_lossy().into_owned()
}
fn str_to_cstring(s: &str) -> Result<SmallCString> {

View File

@ -71,10 +71,7 @@ impl Session<'_> {
use std::str;
let boxed_filter: *mut F = p_arg as *mut F;
let tbl_name = {
let c_slice = CStr::from_ptr(tbl_str).to_bytes();
str::from_utf8(c_slice)
};
let tbl_name = CStr::from_ptr(tbl_str).to_str();
c_int::from(
catch_unwind(|| (*boxed_filter)(tbl_name.expect("non-utf8 table name")))
.unwrap_or_default(),
@ -700,10 +697,7 @@ where
use std::str;
let tuple: *mut (Option<F>, C) = p_ctx as *mut (Option<F>, C);
let tbl_name = {
let c_slice = CStr::from_ptr(tbl_str).to_bytes();
str::from_utf8(c_slice)
};
let tbl_name = CStr::from_ptr(tbl_str).to_str();
match *tuple {
(Some(ref filter), _) => c_int::from(
catch_unwind(|| filter(tbl_name.expect("illegal table name"))).unwrap_or_default(),

View File

@ -441,7 +441,8 @@ impl Statement<'_> {
#[inline]
pub fn parameter_name(&self, index: usize) -> Option<&'_ str> {
self.stmt.bind_parameter_name(index as i32).map(|name| {
str::from_utf8(name.to_bytes()).expect("Invalid UTF-8 sequence in parameter name")
name.to_str()
.expect("Invalid UTF-8 sequence in parameter name")
})
}
@ -766,7 +767,7 @@ impl fmt::Debug for Statement<'_> {
let sql = if self.stmt.is_null() {
Ok("")
} else {
str::from_utf8(self.stmt.sql().unwrap().to_bytes())
self.stmt.sql().unwrap().to_str()
};
f.debug_struct("Statement")
.field("conn", self.conn)

View File

@ -27,10 +27,9 @@ use crate::Connection;
#[cfg(not(feature = "loadable_extension"))]
pub unsafe fn config_log(callback: Option<fn(c_int, &str)>) -> crate::Result<()> {
extern "C" fn log_callback(p_arg: *mut c_void, err: c_int, msg: *const c_char) {
let c_slice = unsafe { CStr::from_ptr(msg).to_bytes() };
let s = unsafe { CStr::from_ptr(msg).to_string_lossy() };
let callback: fn(c_int, &str) = unsafe { mem::transmute(p_arg) };
let s = String::from_utf8_lossy(c_slice);
drop(catch_unwind(|| callback(err, &s)));
}
@ -72,8 +71,7 @@ impl Connection {
pub fn trace(&mut self, trace_fn: Option<fn(&str)>) {
unsafe extern "C" fn trace_callback(p_arg: *mut c_void, z_sql: *const c_char) {
let trace_fn: fn(&str) = mem::transmute(p_arg);
let c_slice = CStr::from_ptr(z_sql).to_bytes();
let s = String::from_utf8_lossy(c_slice);
let s = CStr::from_ptr(z_sql).to_string_lossy();
drop(catch_unwind(|| trace_fn(&s)));
}
@ -100,8 +98,7 @@ impl Connection {
nanoseconds: u64,
) {
let profile_fn: fn(&str, Duration) = mem::transmute(p_arg);
let c_slice = CStr::from_ptr(z_sql).to_bytes();
let s = String::from_utf8_lossy(c_slice);
let s = CStr::from_ptr(z_sql).to_string_lossy();
const NANOS_PER_SEC: u64 = 1_000_000_000;
let duration = Duration::new(