Skip to content

Commit

Permalink
Improve conversion of the received mysql value bytes to MYSQL_TIME
Browse files Browse the repository at this point in the history
Clippy (rightful) complained that we read from a potential unaligned pointer,
which would be undefined behaviour.
The problem is: Clippy continues to complain about this, even if we
use `read_unaligned()` here. This seems to be a clippy bug, see
rust-lang/rust-clippy#2881
  • Loading branch information
weiznich committed Feb 19, 2020
1 parent b1bf5a9 commit 538be52
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions diesel/src/mysql/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,20 @@ impl<'a> MysqlValue<'a> {

/// Checks that the type code is valid, and interprets the data as a
/// `MYSQL_TIME` pointer
#[allow(dead_code)]
// We use `ptr.read_unaligned()` to read the potential unaligned ptr,
// so clippy is clearly wrong here
// https://github.com/rust-lang/rust-clippy/issues/2881
#[allow(dead_code, clippy::cast_ptr_alignment)]
pub(crate) fn time_value(&self) -> deserialize::Result<ffi::MYSQL_TIME> {
match self.tpe.data_type {
MysqlType::Time | MysqlType::Date | MysqlType::DateTime | MysqlType::Timestamp => {
Ok(*unsafe { &*(self.raw as *const _ as *const ffi::MYSQL_TIME) })
let ptr = self.raw.as_ptr() as *const ffi::MYSQL_TIME;
let result = unsafe { ptr.read_unaligned() };
if result.neg == 0 {
Ok(result)
} else {
Err("Negative dates/times are not yet supported".into())
}
}
_ => Err(self.invalid_type_code("timestamp")),
}
Expand Down

0 comments on commit 538be52

Please sign in to comment.