Skip to content

bpo-16512: one jpeg image added with different profile, code modified to detect all types of jpg #8322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/imghdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def what(file, h=None):

def test_jpeg(h, f):
"""JPEG data in JFIF or Exif format"""
if h[6:10] in (b'JFIF', b'Exif'):
if h.startswith(b'\xff\xd8'):
return 'jpeg'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While technically this test is correct, there is a risk to have false positive. Would it be possible to design a stricter test?

My Hachoir parser for JPEG picture checks that it's able to parse at least 3 chunks:
https://github.com/vstinner/hachoir/blob/15f61aef34115a7ed1e76efc84b9176be55032a4/hachoir/parser/image/jpeg.py#L565-L578

A chunk must start with 0xFF, then have a tag in the 0xD0..0xD9 range.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vstinner Hi,

I think I understand your code. For example in b'\xff\xd8\xff\xdb\x00C....':

  1. First 2 bytes must equal to '\xff\xd8'
  2. Then you are looking for the tag (defined in JpegChunk.TAG_INFO) in the next 3 bytes( i.e. '\xff\xdb\x00C').

Am I correct?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vstinner Hi,

I had trouble understainding the code you provided. Can you please elebroate more?
Thanks.


tests.append(test_jpeg)
Expand Down
Binary file added Lib/test/imghdrdata/python2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion Lib/test/test_imghdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
('python.pgm', 'pgm'),
('python.pbm', 'pbm'),
('python.jpg', 'jpeg'),
('python2.jpeg', 'jpeg'),
('python.ras', 'rast'),
('python.sgi', 'rgb'),
('python.tiff', 'tiff'),
Expand Down Expand Up @@ -79,7 +80,7 @@ def test_bad_args(self):
imghdr.what()
with self.assertRaises(AttributeError):
imghdr.what(None)
with self.assertRaises(TypeError):
with self.assertRaises(AttributeError):
imghdr.what(self.testfile, 1)
with self.assertRaises(AttributeError):
imghdr.what(os.fsencode(self.testfile))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed bug in ``imghdr`` that was causing some jpeg image files not being detected as such.