1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use super::types::date_and_time::MysqlTime;
use super::MysqlType;
use crate::deserialize;
use std::error::Error;
use std::mem::MaybeUninit;
/// Raw mysql value as received from the database
#[derive(Clone, Debug)]
pub struct MysqlValue<'a> {
raw: &'a [u8],
tpe: MysqlType,
}
impl<'a> MysqlValue<'a> {
/// Create a new instance of [MysqlValue] based on a byte buffer
/// and information about the type of the value represented by the
/// given buffer
#[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
pub fn new(raw: &'a [u8], tpe: MysqlType) -> Self {
Self::new_internal(raw, tpe)
}
pub(in crate::mysql) fn new_internal(raw: &'a [u8], tpe: MysqlType) -> Self {
Self { raw, tpe }
}
/// Get the underlying raw byte representation
pub fn as_bytes(&self) -> &'a [u8] {
self.raw
}
/// Get the mysql type of the current value
pub fn value_type(&self) -> MysqlType {
self.tpe
}
/// Checks that the type code is valid, and interprets the data as a
/// `MysqlTime` pointer
// We use `ptr::copy` to read the actual data
// and copy it over to the returned `MysqlTime` instance
#[allow(unsafe_code)] // MaybeUninit + ptr copy
pub(crate) fn time_value(&self) -> deserialize::Result<MysqlTime> {
match self.tpe {
MysqlType::Time | MysqlType::Date | MysqlType::DateTime | MysqlType::Timestamp => {
// we check for the size of the `MYSQL_TIME` type from `mysqlclient_sys` here as
// certain older libmysqlclient and newer libmariadb versions do not have all the
// same fields (and size) as the `MysqlTime` type from diesel. The later one is modeled after
// the type from newer libmysqlclient
self.too_short_buffer(
#[cfg(feature = "mysql")]
std::mem::size_of::<mysqlclient_sys::MYSQL_TIME>(),
#[cfg(not(feature = "mysql"))]
std::mem::size_of::<MysqlTime>(),
"timestamp",
)?;
// To ensure we copy the right number of bytes we need to make sure to copy not more bytes than needed
// for `MysqlTime` and not more bytes than inside of the buffer
let len = std::cmp::min(std::mem::size_of::<MysqlTime>(), self.raw.len());
// Zero is a valid pattern for this type so we are fine with initializing all fields to zero
// If the provided byte buffer is too short we just use 0 as default value
let mut out = MaybeUninit::<MysqlTime>::zeroed();
// Make sure to check that the boolean is an actual bool value, so 0 or 1
// as anything else is UB in rust
let neg_offset = std::mem::offset_of!(MysqlTime, neg);
if neg_offset < self.raw.len()
&& self.raw[neg_offset] != 0
&& self.raw[neg_offset] != 1
{
return Err(
"Received invalid value for `neg` in the `MysqlTime` datastructure".into(),
);
}
let result = unsafe {
// SAFETY: We copy over the bytes from our raw buffer to the `MysqlTime` instance
// This type is correctly aligned and we ensure that we do not copy more bytes than are there
// We are also sure that these ptr do not overlap as they are completely different
// instances
std::ptr::copy_nonoverlapping(
self.raw.as_ptr(),
out.as_mut_ptr() as *mut u8,
len,
);
// SAFETY: all zero is a valid pattern for this type
// Otherwise any other bit pattern is also valid, beside
// neg being something other than 0 or 1
// We check for that above by looking at the byte before copying
out.assume_init()
};
if result.neg {
Err("Negative dates/times are not yet supported".into())
} else {
Ok(result)
}
}
_ => Err(self.invalid_type_code("timestamp")),
}
}
/// Returns the numeric representation of this value, based on the type code.
/// Returns an error if the type code is not numeric.
pub(crate) fn numeric_value(&self) -> deserialize::Result<NumericRepresentation<'_>> {
Ok(match self.tpe {
MysqlType::UnsignedTiny | MysqlType::Tiny => {
NumericRepresentation::Tiny(self.raw[0].try_into()?)
}
MysqlType::UnsignedShort | MysqlType::Short => {
self.too_short_buffer(2, "Short")?;
NumericRepresentation::Small(i16::from_ne_bytes((&self.raw[..2]).try_into()?))
}
MysqlType::UnsignedLong | MysqlType::Long => {
self.too_short_buffer(4, "Long")?;
NumericRepresentation::Medium(i32::from_ne_bytes((&self.raw[..4]).try_into()?))
}
MysqlType::UnsignedLongLong | MysqlType::LongLong => {
self.too_short_buffer(8, "LongLong")?;
NumericRepresentation::Big(i64::from_ne_bytes(self.raw.try_into()?))
}
MysqlType::Float => {
self.too_short_buffer(4, "Float")?;
NumericRepresentation::Float(f32::from_ne_bytes(self.raw.try_into()?))
}
MysqlType::Double => {
self.too_short_buffer(8, "Double")?;
NumericRepresentation::Double(f64::from_ne_bytes(self.raw.try_into()?))
}
MysqlType::Numeric => NumericRepresentation::Decimal(self.raw),
_ => return Err(self.invalid_type_code("number")),
})
}
fn invalid_type_code(&self, expected: &str) -> Box<dyn Error + Send + Sync> {
format!(
"Invalid representation received for {}: {:?}",
expected, self.tpe
)
.into()
}
fn too_short_buffer(&self, expected: usize, tpe: &'static str) -> deserialize::Result<()> {
if self.raw.len() < expected {
Err(format!(
"Received a buffer with an invalid size while trying \
to read a {tpe} value: Expected at least {expected} bytes \
but got {}",
self.raw.len()
)
.into())
} else {
Ok(())
}
}
}
/// Represents all possible forms MySQL transmits integers
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum NumericRepresentation<'a> {
/// Corresponds to `MYSQL_TYPE_TINY`
Tiny(i8),
/// Corresponds to `MYSQL_TYPE_SHORT`
Small(i16),
/// Corresponds to `MYSQL_TYPE_INT24` and `MYSQL_TYPE_LONG`
Medium(i32),
/// Corresponds to `MYSQL_TYPE_LONGLONG`
Big(i64),
/// Corresponds to `MYSQL_TYPE_FLOAT`
Float(f32),
/// Corresponds to `MYSQL_TYPE_DOUBLE`
Double(f64),
/// Corresponds to `MYSQL_TYPE_DECIMAL` and `MYSQL_TYPE_NEWDECIMAL`
Decimal(&'a [u8]),
}
#[test]
#[allow(unsafe_code, reason = "Test code")]
fn invalid_reads() {
use crate::data_types::MysqlTimestampType;
assert!(MysqlValue::new_internal(&[1], MysqlType::Timestamp)
.time_value()
.is_err());
let v = MysqlTime {
year: 2025,
month: 9,
day: 15,
hour: 22,
minute: 3,
second: 10,
second_part: 0,
neg: false,
time_type: MysqlTimestampType::MYSQL_TIMESTAMP_DATETIME,
time_zone_displacement: 0,
};
let mut bytes = [0; std::mem::size_of::<MysqlTime>()];
unsafe {
// SAFETY: Test code
// also the size matches and we want to get raw bytes
std::ptr::copy(
&v as *const MysqlTime as *const u8,
bytes.as_mut_ptr(),
bytes.len(),
);
}
let offset = std::mem::offset_of!(MysqlTime, neg);
bytes[offset] = 42;
assert!(MysqlValue::new_internal(&bytes, MysqlType::Timestamp)
.time_value()
.is_err());
assert!(MysqlValue::new_internal(&[1, 2], MysqlType::Long)
.numeric_value()
.is_err());
assert!(MysqlValue::new_internal(&[1, 2, 3, 4], MysqlType::LongLong)
.numeric_value()
.is_err());
assert!(MysqlValue::new_internal(&[1], MysqlType::Short)
.numeric_value()
.is_err());
assert!(MysqlValue::new_internal(&[1, 2, 3, 4], MysqlType::Double)
.numeric_value()
.is_err());
assert!(MysqlValue::new_internal(&[1, 2], MysqlType::Float)
.numeric_value()
.is_err());
assert!(MysqlValue::new_internal(&[1], MysqlType::Tiny)
.numeric_value()
.is_ok());
}