dwgoon / jpegio

A python package for accessing the internal variables of JPEG file format such as DCT coefficients and quantization tables
Apache License 2.0
73 stars 18 forks source link

Graceful Exiting #7

Open asrabon opened 4 years ago

asrabon commented 4 years ago

Is there anyway to make the jpegio return a exception if it breaks in the libjpeg library? Currently if you try to read a jpeg that it can not parse properly or is not actually a jpeg file it will kill the entire program without being able to recover.

dwgoon commented 4 years ago

@asrabon Could you demonstrate examples or cases in which an exception needs to be raised? It will be helpful for me to refine the codes if I know the cases.

asrabon commented 4 years ago

I can give you some more examples if you need them but these should give you a general idea of what I am talking about:

  1. In libjpeg/src/jchuff.c: If the huffman table that is referenced is negative or larger than the maximum number of huffman tables then it runs ERREXIT1 which kills the entire program without being able to recover it. if (tblno < 0 || tblno >= NUM_HUFF_TBLS) ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  2. In libjpeg/src/jdmarker.c: If there is no SOI(Start of Image) marker then it runs ERREXIT2 which kills the entire program. if (c != 0xFF || c2 != (int) M_SOI) ERREXIT2(cinfo, JERR_NO_SOI, c, c2);

It would be nice everywhere libjpeg throws an ERREXIT of any kind it returned a python exception or error code rather than terminating the entire program by calling the exit function in C. If you wanted a trivial script you could run to see one example of the unrecoverable program termination.

from jpegio import read

jpeg = read("/path/to/some/file.gif") print("Will never reach this print statement.")

dwgoon commented 4 years ago

@asrabon Thanks for your explanation. I am gonna try dealing with it .