Skip to content

Commit

Permalink
fix: Detect corrupted files, exit on exception (DEV-4457)
Browse files Browse the repository at this point in the history
  • Loading branch information
siers committed Dec 17, 2024
1 parent e855b19 commit 2991e89
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/formats/SipiIOJpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ bool SipiIOJpeg::read(SipiImage *img,
img->pixels = new byte[img->ny * sll];

try {
cinfo.err->msg_code = 0;
linbuf = (*cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo, JPOOL_IMAGE, sll, 1);
for (size_t i = 0; i < img->ny; i++) {
jpeg_read_scanlines(&cinfo, linbuf, 1);
Expand All @@ -752,6 +753,14 @@ bool SipiIOJpeg::read(SipiImage *img,
close(infile);
throw SipiImageError("Error reading JPEG file: \"" + filepath + "\": " + jpgerr.what());
}

if (cinfo.err->msg_code == JWRN_HIT_MARKER) {
jpeg_destroy_decompress(&cinfo);
close(infile);
throw SipiImageError(
"Error reading JPEG file: corrupt JPEG data reported by libjpeg, code " + std::to_string(cinfo.err->msg_code));
}

try {
jpeg_finish_decompress(&cinfo);
} catch (JpegError &jpgerr) {
Expand Down
5 changes: 3 additions & 2 deletions src/sipi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
* \brief Implements an IIIF server with many features.
*
*/
#include <syslog.h>
#include <csignal>
#include <dirent.h>
#include <execinfo.h>
#include <iostream>
#include <csignal>
#include <sstream>
#include <string>
#include <sys/stat.h>
#include <syslog.h>

#include <thread>
#include <unistd.h>
Expand Down Expand Up @@ -862,6 +862,7 @@ int main(int argc, char *argv[])
}
} catch (Sipi::SipiImageError &err) {
std::cerr << err << std::endl;
exit(1);
}

//
Expand Down

0 comments on commit 2991e89

Please sign in to comment.