Skip to content

Commit 2ad9b54

Browse files
committed
Avoid panic in slice cast
1 parent b56e30d commit 2ad9b54

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ description = "Low-level asynchronous TIFF reader."
99
readme = "README.md"
1010

1111
[dependencies]
12-
bytemuck = "1.24.0"
12+
bytemuck = { version = "1.24.0", features = ["extern_crate_std"] }
1313
byteorder = "1"
1414
bytes = "1.7.0"
1515
flate2 = "1.0.20"

src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ pub enum AsyncTiffError {
4242
#[error(transparent)]
4343
ReqwestError(#[from] reqwest::Error),
4444

45+
/// Bytemuck error
46+
#[error(transparent)]
47+
BytemuckError(#[from] bytemuck::PodCastError),
48+
4549
/// External error
4650
#[error(transparent)]
4751
External(Box<dyn std::error::Error + Send + Sync>),

src/metadata/fetch.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<'a, F: MetadataFetch> MetadataCursor<'a, F> {
121121
/// Read `n` i8s from the cursor, advancing the internal state by `n` bytes.
122122
pub(crate) async fn read_i8_n(&mut self, n: u64) -> AsyncTiffResult<Value> {
123123
let (buf, _endianness) = self.read(n).await?.into_inner();
124-
let values: &[i8] = bytemuck::cast_slice(&buf);
124+
let values: &[i8] = bytemuck::try_cast_slice(&buf)?;
125125
Ok(Value::List(
126126
values.iter().copied().map(Value::SignedByte).collect(),
127127
))
@@ -139,7 +139,7 @@ impl<'a, F: MetadataFetch> MetadataCursor<'a, F> {
139139
// If the endianness matches the machine endianness, we can do a direct cast.
140140
if self.endianness == MACHINE_ENDIANNESS {
141141
let (buf, _endianness) = reader.into_inner();
142-
let values: &[u16] = bytemuck::cast_slice(&buf);
142+
let values: &[u16] = bytemuck::try_cast_slice(&buf)?;
143143
Ok(Value::List(
144144
values.iter().copied().map(Value::Short).collect(),
145145
))
@@ -159,7 +159,7 @@ impl<'a, F: MetadataFetch> MetadataCursor<'a, F> {
159159
// If the endianness matches the machine endianness, we can do a direct cast.
160160
if self.endianness == MACHINE_ENDIANNESS {
161161
let (buf, _endianness) = reader.into_inner();
162-
let values: &[i16] = bytemuck::cast_slice(&buf);
162+
let values: &[i16] = bytemuck::try_cast_slice(&buf)?;
163163
Ok(Value::List(
164164
values.iter().copied().map(Value::SignedShort).collect(),
165165
))
@@ -184,7 +184,7 @@ impl<'a, F: MetadataFetch> MetadataCursor<'a, F> {
184184
// If the endianness matches the machine endianness, we can do a direct cast.
185185
if self.endianness == MACHINE_ENDIANNESS {
186186
let (buf, _endianness) = reader.into_inner();
187-
let values: &[u32] = bytemuck::cast_slice(&buf);
187+
let values: &[u32] = bytemuck::try_cast_slice(&buf)?;
188188
Ok(Value::List(
189189
values.iter().copied().map(Value::Unsigned).collect(),
190190
))
@@ -204,7 +204,7 @@ impl<'a, F: MetadataFetch> MetadataCursor<'a, F> {
204204
// If the endianness matches the machine endianness, we can do a direct cast.
205205
if self.endianness == MACHINE_ENDIANNESS {
206206
let (buf, _endianness) = reader.into_inner();
207-
let values: &[u32] = bytemuck::cast_slice(&buf);
207+
let values: &[u32] = bytemuck::try_cast_slice(&buf)?;
208208
Ok(Value::List(
209209
values.iter().copied().map(Value::Ifd).collect(),
210210
))
@@ -229,7 +229,7 @@ impl<'a, F: MetadataFetch> MetadataCursor<'a, F> {
229229
// If the endianness matches the machine endianness, we can do a direct cast.
230230
if self.endianness == MACHINE_ENDIANNESS {
231231
let (buf, _endianness) = reader.into_inner();
232-
let values: &[i32] = bytemuck::cast_slice(&buf);
232+
let values: &[i32] = bytemuck::try_cast_slice(&buf)?;
233233
Ok(Value::List(
234234
values.iter().copied().map(Value::Signed).collect(),
235235
))
@@ -254,7 +254,7 @@ impl<'a, F: MetadataFetch> MetadataCursor<'a, F> {
254254
// If the endianness matches the machine endianness, we can do a direct cast.
255255
if self.endianness == MACHINE_ENDIANNESS {
256256
let (buf, _endianness) = reader.into_inner();
257-
let values: &[u64] = bytemuck::cast_slice(&buf);
257+
let values: &[u64] = bytemuck::try_cast_slice(&buf)?;
258258
Ok(Value::List(
259259
values.iter().copied().map(Value::UnsignedBig).collect(),
260260
))
@@ -274,7 +274,7 @@ impl<'a, F: MetadataFetch> MetadataCursor<'a, F> {
274274
// If the endianness matches the machine endianness, we can do a direct cast.
275275
if self.endianness == MACHINE_ENDIANNESS {
276276
let (buf, _endianness) = reader.into_inner();
277-
let values: &[u64] = bytemuck::cast_slice(&buf);
277+
let values: &[u64] = bytemuck::try_cast_slice(&buf)?;
278278
Ok(Value::List(
279279
values.iter().copied().map(Value::IfdBig).collect(),
280280
))
@@ -299,7 +299,7 @@ impl<'a, F: MetadataFetch> MetadataCursor<'a, F> {
299299
// If the endianness matches the machine endianness, we can do a direct cast.
300300
if self.endianness == MACHINE_ENDIANNESS {
301301
let (buf, _endianness) = reader.into_inner();
302-
let values: &[i64] = bytemuck::cast_slice(&buf);
302+
let values: &[i64] = bytemuck::try_cast_slice(&buf)?;
303303
Ok(Value::List(
304304
values.iter().copied().map(Value::SignedBig).collect(),
305305
))
@@ -319,7 +319,7 @@ impl<'a, F: MetadataFetch> MetadataCursor<'a, F> {
319319
// If the endianness matches the machine endianness, we can do a direct cast.
320320
if self.endianness == MACHINE_ENDIANNESS {
321321
let (buf, _endianness) = reader.into_inner();
322-
let values: &[f32] = bytemuck::cast_slice(&buf);
322+
let values: &[f32] = bytemuck::try_cast_slice(&buf)?;
323323
Ok(Value::List(
324324
values.iter().copied().map(Value::Float).collect(),
325325
))
@@ -343,7 +343,7 @@ impl<'a, F: MetadataFetch> MetadataCursor<'a, F> {
343343
// If the endianness matches the machine endianness, we can do a direct cast.
344344
if self.endianness == MACHINE_ENDIANNESS {
345345
let (buf, _endianness) = reader.into_inner();
346-
let values: &[f64] = bytemuck::cast_slice(&buf);
346+
let values: &[f64] = bytemuck::try_cast_slice(&buf)?;
347347
Ok(Value::List(
348348
values.iter().copied().map(Value::Double).collect(),
349349
))

0 commit comments

Comments
 (0)