Skip to content

Commit 91e2914

Browse files
authored
Change validation to a debug assert (oxipng#481)
1 parent a3b104a commit 91e2914

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/lib.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -657,28 +657,9 @@ fn optimize_png(
657657
);
658658
}
659659

660-
let (old_png, new_png) = rayon::join(
661-
|| load_png_image_from_memory(original_data),
662-
|| load_png_image_from_memory(&output),
663-
);
664-
665-
if let Ok(new_png) = new_png {
666-
if let Ok(old_png) = old_png {
667-
if images_equal(&old_png, &new_png) {
668-
return Ok(output);
669-
}
670-
} else {
671-
// The original image might be invalid if, for example, there is a CRC error,
672-
// and we set fix_errors to true. In that case, all we can do is check that the
673-
// new image is decodable.
674-
return Ok(output);
675-
}
676-
}
660+
debug_assert!(validate_output(&output, original_data));
677661

678-
error!(
679-
"The resulting image is corrupted and will not be outputted.\nThis is a bug! Please report it at https://github.com/shssoichiro/oxipng/issues"
680-
);
681-
Err(PngError::new("The resulting image is corrupted"))
662+
Ok(output)
682663
}
683664

684665
fn perform_reductions(
@@ -1061,6 +1042,29 @@ fn copy_times(input_path_meta: &Metadata, out_path: &Path) -> PngResult<()> {
10611042
})
10621043
}
10631044

1045+
/// Validate that the output png data still matches the original image
1046+
fn validate_output(output: &[u8], original_data: &[u8]) -> bool {
1047+
let (old_png, new_png) = rayon::join(
1048+
|| load_png_image_from_memory(original_data),
1049+
|| load_png_image_from_memory(output),
1050+
);
1051+
1052+
match (new_png, old_png) {
1053+
(Err(new_err), _) => {
1054+
error!("Failed to read output image for validation: {}", new_err);
1055+
false
1056+
}
1057+
(_, Err(old_err)) => {
1058+
// The original image might be invalid if, for example, there is a CRC error,
1059+
// and we set fix_errors to true. In that case, all we can do is check that the
1060+
// new image is decodable.
1061+
warn!("Failed to read input image for validation: {}", old_err);
1062+
true
1063+
}
1064+
(Ok(new_png), Ok(old_png)) => images_equal(&old_png, &new_png),
1065+
}
1066+
}
1067+
10641068
/// Loads a PNG image from memory to a [DynamicImage]
10651069
fn load_png_image_from_memory(png_data: &[u8]) -> Result<DynamicImage, image::ImageError> {
10661070
let mut reader = image::io::Reader::new(Cursor::new(png_data));

0 commit comments

Comments
 (0)