Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The scope of the unsafe block can be appropriately reduced #3160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion diesel/src/mysql/connection/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ impl RawConnection {
}

fn consume_current_result(&self) -> QueryResult<()> {
let res = unsafe { ffi::mysql_store_result(self.0.as_ptr()) };
unsafe {
let res = ffi::mysql_store_result(self.0.as_ptr());
if !res.is_null() {
ffi::mysql_free_result(res);
}
Expand Down
8 changes: 3 additions & 5 deletions diesel/src/mysql/connection/stmt/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ impl StatementMetadata {
}

pub(in crate::mysql::connection) fn fields(&'_ self) -> &'_ [MysqlFieldMetadata<'_>] {
unsafe {
let num_fields = ffi::mysql_num_fields(self.result.as_ptr());
let field_ptr = ffi::mysql_fetch_fields(self.result.as_ptr());
let num_fields = unsafe { ffi::mysql_num_fields(self.result.as_ptr()) };
let field_ptr = unsafe { ffi::mysql_fetch_fields(self.result.as_ptr()) };
if field_ptr.is_null() {
&[]
} else {
slice::from_raw_parts(field_ptr as _, num_fields as usize)
unsafe { slice::from_raw_parts(field_ptr as _, num_fields as usize) }
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion diesel/src/mysql/types/date_and_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ macro_rules! mysql_time_impls {
#[cfg(feature = "mysql_backend")]
impl ToSql<$ty, Mysql> for MysqlTime {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Mysql>) -> serialize::Result {
let bytes_ptr = self as *const MysqlTime as *const u8;
let bytes = unsafe {
Comment on lines +116 to 117
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one of the relevant change in this PR.

let bytes_ptr = self as *const MysqlTime as *const u8;
slice::from_raw_parts(bytes_ptr, mem::size_of::<MysqlTime>())
};
out.write_all(bytes)?;
Expand Down
8 changes: 3 additions & 5 deletions diesel/src/pg/connection/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,9 @@ impl Drop for RawConnection {
}

fn last_error_message(conn: *const PGconn) -> String {
unsafe {
let error_ptr = PQerrorMessage(conn);
let bytes = CStr::from_ptr(error_ptr).to_bytes();
String::from_utf8_lossy(bytes).to_string()
}
let error_ptr = unsafe { PQerrorMessage(conn) };
let bytes = unsafe { CStr::from_ptr(error_ptr).to_bytes() };
String::from_utf8_lossy(bytes).to_string()
}

/// Internal wrapper around a `*mut PGresult` which is known to be not-null, and
Expand Down
8 changes: 3 additions & 5 deletions diesel/src/pg/connection/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,17 @@ impl PgResult {
}

pub(super) fn rows_affected(&self) -> usize {
unsafe {
let count_char_ptr = PQcmdTuples(self.internal_result.as_ptr());
let count_bytes = CStr::from_ptr(count_char_ptr).to_bytes();
let count_char_ptr = unsafe { PQcmdTuples(self.internal_result.as_ptr()) };
let count_bytes = unsafe { CStr::from_ptr(count_char_ptr).to_bytes() };
// Using from_utf8_unchecked is ok here because, we've set the
// client encoding to utf8
let count_str = str::from_utf8_unchecked(count_bytes);
let count_str = unsafe { str::from_utf8_unchecked(count_bytes) };
match count_str {
"" => 0,
_ => count_str
.parse()
.expect("Error parsing `rows_affected` as integer value"),
}
}
}

pub(super) fn num_rows(&self) -> usize {
Expand Down
9 changes: 4 additions & 5 deletions diesel/src/sqlite/connection/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,11 @@ where
ffi::sqlite3_aggregate_context(ctx, std::mem::size_of::<OptionalAggregator<A>>() as i32)
};
let aggregate_context = NonNull::new(aggregate_context as *mut OptionalAggregator<A>);
let aggregator = unsafe {
match aggregate_context.map(|a| &mut *a.as_ptr()) {
let aggregator =
match unsafe { aggregate_context.map(|a| &mut *a.as_ptr()) } {
Some(&mut OptionalAggregator::Some(ref mut agg)) => agg,
Some(a_ptr @ &mut OptionalAggregator::None) => {
ptr::write_unaligned(a_ptr as *mut _, OptionalAggregator::Some(A::default()));
unsafe { ptr::write_unaligned(a_ptr as *mut _, OptionalAggregator::Some(A::default())) };
if let OptionalAggregator::Some(ref mut agg) = a_ptr {
agg
} else {
Expand All @@ -417,8 +417,7 @@ where
None => {
return Err(SqliteCallbackError::Abort(NULL_AG_CTX_ERR));
}
}
};
};
let args = build_sql_function_args::<ArgsSqlType, Args>(args)?;

aggregator.step(args);
Expand Down