Skip to content

Commit 1839bbd

Browse files
authored
Fix panic caused by unsupported TIFF format (#287)
TIFF sample format IEEEFP is currently not supported by `image` crate. Returning an error instead of calling `unwrap()` allows Python code to catch it.
1 parent 544dba1 commit 1839bbd

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/processes/macos_icons.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,28 @@ impl IconCache {
3131
tiff.hash(&mut hasher);
3232
let tiff_hash = hasher.finish();
3333
e.insert(tiff_hash);
34-
let icon = self
35-
.icons
36-
.entry(tiff_hash)
37-
.or_insert_with(|| tiff_to_png(&tiff));
34+
let icon = match self.icons.entry(tiff_hash) {
35+
Entry::Occupied(e) => e.into_mut(),
36+
Entry::Vacant(e) => {
37+
let png = tiff_to_png(&tiff)?;
38+
e.insert(png)
39+
}
40+
};
3841
Ok(icon)
3942
}
4043
}
4144
}
4245
}
4346

44-
pub fn tiff_to_png(tiff: &[u8]) -> Vec<u8> {
47+
pub fn tiff_to_png(tiff: &[u8]) -> Result<Vec<u8>> {
4548
let mut c = Cursor::new(Vec::new());
46-
let tiff_image = image::load_from_memory_with_format(tiff, image::ImageFormat::Tiff)
47-
.unwrap()
48-
.resize(32, 32, image::imageops::FilterType::Triangle);
49-
tiff_image
50-
.write_to(&mut c, image::ImageFormat::Png)
51-
.unwrap();
52-
c.into_inner()
49+
let tiff_image = image::load_from_memory_with_format(tiff, image::ImageFormat::Tiff)?.resize(
50+
32,
51+
32,
52+
image::imageops::FilterType::Triangle,
53+
);
54+
tiff_image.write_to(&mut c, image::ImageFormat::Png)?;
55+
Ok(c.into_inner())
5356
}
5457

5558
pub fn tiff_data_for_executable(executable: &Path) -> Result<Vec<u8>> {

0 commit comments

Comments
 (0)