LeoHsiao1 / pyexiv2

Read and write image metadata, including EXIF, IPTC, XMP, ICC Profile.
GNU General Public License v3.0
202 stars 39 forks source link

Segmentation fault on some images with 2.14 #145

Open estribiyo opened 1 month ago

estribiyo commented 1 month ago

When trying to read exif of some images (example attached)

filename = 'img_04467.jpg'
encoding = 'utf8'
img = pyexiv2.Image(filename, encoding)
exif = img.read_exif()

img_04465

(Works ok with same code and image on 2.12)

estribiyo commented 1 month ago

I'm using Linux (Debian Bookworm) with standard libraries. Let me know if you need more information or any other test.

Thank you

LeoHsiao1 commented 1 month ago

Hi! If you upload an image directly to GitHub, GitHub may automatically modify this image to be more suitable for web delivery. So I recommend sharing the image in a zip file. The good news is that I downloaded your image and reproduced the error.

pyexiv2==2.12 can read metadata, but prints a warning:

>>> import pyexiv2
>>> filename = '1.jpg'
>>> encoding = 'utf8'
>>> img = pyexiv2.Image(filename, encoding)
Directory Sony1, entry 0xb2d4 has unknown Exif (TIFF) type 61676; setting type size 1.

>>> len(img.read_iptc())
5
>>> len(img.read_xmp())
3
>>> len(img.read_exif())
57

pyexiv2==2.14 crashes when reading exif:

>>> import pyexiv2
>>> filename = '1.jpg'
>>> encoding = 'utf8'
>>> img = pyexiv2.Image(filename, encoding)
Directory Sony1, entry 0xb2d4 has unknown Exif (TIFF) type 61676; setting type size 1.

>>> len(img.read_iptc())
5
>>> len(img.read_xmp())
3
>>> len(img.read_exif())
<crashed>

Please give me a few days to troubleshoot if it's a bug of pyexiv2.

estribiyo commented 1 month ago

I'll keep that in mind next time...

Don't worry, take the time you need and/or consider... I was just letting you know that I detected the error...

LeoHsiao1 commented 1 month ago

As this warning: Directory Sony1, entry 0xb2d4 has unknown Exif (TIFF) type 61676; setting type size 1.

It is printed by exiv2's code here: https://github.com/Exiv2/exiv2/blob/75c12fb54dd08b6fdec076d983b34a945438a1d6/src/tiffvisitor_int.cpp#L1251 https://github.com/Exiv2/exiv2/blob/75c12fb54dd08b6fdec076d983b34a945438a1d6/include/exiv2/types.hpp#L108

exiv2 has found that Exif.Sony1.0xb2d4 is an unknown tag type. So exiv2 can't read the value of this tag. pyexiv2 triggers a segmentation fault when reading Exif.Sony1.0xb2d4, causing the Python program to crash.

The solution I came up with is to return an empty string if exiv2 doesn't recognize a tag type. The improved results are as follows:

>>> import pyexiv2
>>> img = pyexiv2.Image('1.jpg')  # exiv2 will warn about the unknown tag type
[warn] Directory Sony1, entry 0xb2d4 has unknown Exif (TIFF) type 61676; setting type size 1.

>>> img.read_exif()['Exif.Sony1.0xb2d4']   # reading the unknown tag type will return an empty string
''

When you modify the tag, pyexiv2 follows a simple logic: If you assign a string to a tag, the tag will be saved as a string type, regardless of the tag's original type.

>>> img.modify_exif({'Exif.Sony1.0xb2d4': 'test'})
[warn] Directory Sony1, entry 0xb2d4 has unknown Exif (TIFF) type 61676; setting type size 1.

>>> img.read_exif()['Exif.Sony1.0xb2d4']
'test'     # the tag is saved as a string type

How about this solution? After all, pyexiv2 can't parse the unknown tag type.

LeoHsiao1 commented 1 month ago

Here are all the exif tags read from your image:

{
    'Exif.Image.ProcessingSoftware': 'Miniaturizador - 0.5.67',
    'Exif.Image.ImageDescription': '',
    'Exif.Image.Make': 'SONY',
    'Exif.Image.Model': 'DSC-P8',
    'Exif.Image.Orientation': '1',
    'Exif.Image.XResolution': '72/1',
    'Exif.Image.YResolution': '72/1',
    'Exif.Image.ResolutionUnit': '2',
    'Exif.Image.DateTime': '2003: 06: 04 00: 22: 11',
    'Exif.Image.Artist': 'Desconocido',
    'Exif.Image.YCbCrPositioning': '2',
    'Exif.Image.Copyright': 'Desconocido',
    'Exif.Image.ExifTag': '308',
    'Exif.Photo.ExposureTime': '10/400',
    'Exif.Photo.FNumber': '28/10',
    'Exif.Photo.ExposureProgram': '2',
    'Exif.Photo.ISOSpeedRatings': '160',
    'Exif.Photo.ExifVersion': '48 50 50 48',
    'Exif.Photo.DateTimeOriginal': '2003: 06: 04 00: 22: 11',
    'Exif.Photo.DateTimeDigitized': '2003: 06: 04 00: 22: 11',
    'Exif.Photo.ComponentsConfiguration': '1 2 3 0',
    'Exif.Photo.CompressedBitsPerPixel': '4/1',
    'Exif.Photo.ExposureBiasValue': '0/10',
    'Exif.Photo.MaxApertureValue': '48/16',
    'Exif.Photo.MeteringMode': '5',
    'Exif.Photo.LightSource': '0',
    'Exif.Photo.Flash': '15',
    'Exif.Photo.FocalLength': '60/10',
    'Exif.Photo.MakerNote': '83 79 78 89 32 68 83 67 32 0 0 0 9 0 1 144 7 0 148 0 0 0 80 3 0 0 2 144 7 0 200 0 0 0 228 3 0 0 3 144 7 0 200 0 0 0 172 4 0 0 4 144 7 0 26 0 0 0 116 5 0 0 5 144 7 0 120 0 0 0 142 5 0 0 6 144 7 0 252 0 0 0 6 6 0 0 7 144 7 0 200 0 0 0 2 7 0 0 8 144 7 0 200 0 0 0 202 7 0 0 212 178 236 240 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 182 0 1 0 231 0 221 58 235 0 30 153 232 0 125 23 0 0 0 0 2 0 0 0 0 0 0 0 2 0 1 81 166 255 0 0 216 0 0 236 15 160 149 255 198 224 125 64 133 14 238 255 251 255 55 0 0 0 74 48 0 136 125 5 74 48 91 136 125 138 74 0 0 0 0 1 19 0 94 0 182 0 0 216 0 0 0 231 148 0 0 94 142 0 0 129 0 136 125 48 91 138 74 0 85 1 135 94 39 216 96 0 0 27 2 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 231 0 64 0 112 0 108 0 0 1 19 1 0 0 246 0 140 2 239 0 140 2 239 0 0 0 216 0 0 0 0 0 0 0 0 0 0 0 0 205 0 0 0 0 0 112 0 112 136 14 89 205 0 0 0 0 0 0 0 0 0 112 143 138 107 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 64 34 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 125 8 93 8 125 0 5 0 0 0 4 1 74 199 0 112 187 112 187 112 136 64 0 0 0 220 220 115 70 1 243 0 204 1 60 1 218 0 238 0 135 0 238 0 116 142 43 82 26 117 145 117 172 19 226 205 113 125 87 33 0 0 71 227 71 227 142 167 40 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 8 48 0 39 0 220 4 1 146 136 0 0 146 136 1 232 1 228 1 1 0 255 4 245 124 250 189 31 161 221 0 0 150 225 215 144 196 133 187 226 76 201 106 219 135 24 92 206 197 200 221 44 1 199 154 249 250 3 134 252 190 57 45 218 108 159 149 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 32 8 27 0 1 220 220 188 95 16 0 0 8 4 8 8 8 0 1 250 0 196 0 196 0 196 0 196 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 64 0 0 0 0 0 85 0 0 0 0 0 0 0 216 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 0 125 64 14 190 105 190 236 146 5 27 8 216 0 0 0 1 0 136 182 0 0 0 0 255 255 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 64 0 64 0 64 0 0 0 1 1 123 1 63 27 98 216 69 27 88 27 75 8 174 1 47 1 223 216 207 86 63 27 15 27 24 27 108 1 71 1 240 231 216 5 155 125 141 8 31 27 87 1 88 1 111 94 166 4 201 64 24 64 234 64 94 0 250 1 232 216 245 216 33 8 138 8 255 27 89 0 70 0 253 1 181 1 209 1 29 1 233 8 29 1 44 8 117 8 70 8 225 8 60 8 135 8 23 0 0 0 0 0 182 0 196 0 211 0 76 0 93 0 0 0 0 0 43 0 115 0 211 0 196 0 121 0 0 0 0 0 208 0 102 0 60 0 80 0 145 0 0 0 0 0 246 0 175 1 96 1 247 0 191 0 0 0 0 0 69 0 19 0 172 0 85 0 243 0 0 0 0 0 1 0 138 0 121 0 186 0 240 0 138 0 32 0 32 0 129 0 57 0 197 0 71 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 57 96 135 227 240 116 197 254 135 37 92 163 47 71 236 138 0 0 0 0 0 163 224 235 43 217 186 172 96 39 224 244 2 194 240 193 92 66 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 138 44 129 27 108 62 12 171 129 225 182 50 86 98 94 106 0 0 0 0 0 163 40 62 211 46 92 126 149 154 146 156 32 146 138 88 4 77 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 234 65 234 65 234 65 234 65 234 65 234 65 234 65 86 52 0 0 0 0 0 163 138 107 138 201 112 234 112 172 112 65 112 204 112 133 112 143 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0',
    'Exif.MakerNote.Offset': '726',
    'Exif.MakerNote.ByteOrder': 'II',
    'Exif.Sony1.0x9001': '0 0 0 0 0 1 0 182 0 1 0 231 0 221 58 235 0 30 153 232 0 125 23 0 0 0 0 2 0 0 0 0 0 0 0 2 0 1 81 166 255 0 0 216 0 0 236 15 160 149 255 198 224 125 64 133 14 238 255 251 255 55 0 0 0 74 48 0 136 125 5 74 48 91 136 125 138 74 0 0 0 0 1 19 0 94 0 182 0 0 216 0 0 0 231 148 0 0 94 142 0 0 129 0 136 125 48 91 138 74 0 85 1 135 94 39 216 96 0 0 27 2 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 231 0 64 0 112 0 108 0 0 1 19',
    'Exif.Sony1.0x9002': '1 0 0 246 0 140 2 239 0 140 2 239 0 0 0 216 0 0 0 0 0 0 0 0 0 0 0 0 205 0 0 0 0 0 112 0 112 136 14 89 205 0 0 0 0 0 0 0 0 0 112 143 138 107 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 64 34 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 125 8 93 8 125 0 5 0 0 0 4 1 74 199 0 112 187 112 187 112 136 64 0 0 0',
    'Exif.Sony1.0x9003': '220 220 115 70 1 243 0 204 1 60 1 218 0 238 0 135 0 238 0 116 142 43 82 26 117 145 117 172 19 226 205 113 125 87 33 0 0 71 227 71 227 142 167 40 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 8 48 0 39 0 220 4 1 146 136 0 0 146 136 1 232 1 228 1 1 0 255 4 245 124 250 189 31 161 221 0 0 150 225 215 144 196 133 187 226 76 201 106 219 135 24 92 206 197 200 221 44 1 199 154 249 250 3 134 252 190 57 45 218 108 159 149 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 189 32 8 27 0 1 220 220 188 95 16',
    'Exif.Sony1.0x9004': '0 0 8 4 8 8 8 0 1 250 0 196 0 196 0 196 0 196 0 0 0 0 0 1 0 0',
    'Exif.Sony1.0x9005': '1 1 0 0 0 0 0 64 0 0 0 0 0 85 0 0 0 0 0 0 0 216 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 0 125 64 14 190 105 190 236 146 5 27 8 216 0 0 0 1 0 136 182 0 0 0 0 255 255 0 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 64 0 64 0 64',
    'Exif.Sony1.0x9006': '0 0 0 1 1 123 1 63 27 98 216 69 27 88 27 75 8 174 1 47 1 223 216 207 86 63 27 15 27 24 27 108 1 71 1 240 231 216 5 155 125 141 8 31 27 87 1 88 1 111 94 166 4 201 64 24 64 234 64 94 0 250 1 232 216 245 216 33 8 138 8 255 27 89 0 70 0 253 1 181 1 209 1 29 1 233 8 29 1 44 8 117 8 70 8 225 8 60 8 135 8 23 0 0 0 0 0 182 0 196 0 211 0 76 0 93 0 0 0 0 0 43 0 115 0 211 0 196 0 121 0 0 0 0 0 208 0 102 0 60 0 80 0 145 0 0 0 0 0 246 0 175 1 96 1 247 0 191 0 0 0 0 0 69 0 19 0 172 0 85 0 243 0 0 0 0 0 1 0 138 0 121 0 186 0 240 0 138 0 32 0 32 0 129 0 57 0 197 0 71 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0',
    'Exif.Sony1.0x9007': '0 0 57 96 135 227 240 116 197 254 135 37 92 163 47 71 236 138 0 0 0 0 0 163 224 235 43 217 186 172 96 39 224 244 2 194 240 193 92 66 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 138 44 129 27 108 62 12 171 129 225 182 50 86 98 94 106 0 0 0 0 0 163 40 62 211 46 92 126 149 154 146 156 32 146 138 88 4 77 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 234 65 234 65 234 65 234 65 234 65 234 65 234 65 86 52 0 0 0 0 0 163 138 107 138 201 112 234 112 172 112 65 112 204 112 133 112 143 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0',
    'Exif.Sony1.0x9008': '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0',
    'Exif.Sony1.0xb2d4': '',
    'Exif.Photo.FlashpixVersion': '48 49 48 48',
    'Exif.Photo.ColorSpace': '1',
    'Exif.Photo.PixelXDimension': '640',
    'Exif.Photo.PixelYDimension': '480',
    'Exif.Photo.InteroperabilityTag': '2194',
    'Exif.Iop.InteroperabilityIndex': 'R98',
    'Exif.Iop.InteroperabilityVersion': '48 49 48 48',
    'Exif.Photo.FileSource': '3',
    'Exif.Photo.SceneType': '1',
    'Exif.Photo.CustomRendered': '0',
    'Exif.Photo.ExposureMode': '0',
    'Exif.Photo.WhiteBalance': '0',
    'Exif.Photo.SceneCaptureType': '0',
    'Exif.Image.PrintImageMatching': '80 114 105 110 116 73 77 0 48 50 53 48 0 0 2 0 2 0 1 0 0 0 1 1 0 0 0 0',
    'Exif.Thumbnail.Compression': '6',
    'Exif.Thumbnail.JPEGInterchangeFormat': '2266',
    'Exif.Thumbnail.JPEGInterchangeFormatLength': '3780'
}

As you can see, these tags about Sony1. Even though their values are read, they are numbers, not ASCII strings. Maybe only Sony's engineers, know what these numbers mean.

LeoHsiao1 commented 1 month ago

I used another tool to view the image:

$ exiftool -v 1.jpg
JPEG APP1 (6052 bytes):
  ExifByteOrder = II
  + [IFD0 directory with 14 entries]
  | 0)  ProcessingSoftware = Miniaturizador - 0.5.67
  | 1)  ImageDescription =
  | 2)  Make = SONY
  | 3)  Model = DSC-P8
  | 4)  Orientation = 1
  | 5)  XResolution = 72 (72/1)
  | 6)  YResolution = 72 (72/1)
  | 7)  ResolutionUnit = 2
  | 8)  ModifyDate = 2003:06:04 00:22:11
  | 9)  Artist = Desconocido
  | 10) YCbCrPositioning = 2
  | 11) Copyright = Desconocido
  | 12) ExifOffset (SubDirectory) -->
  | + [ExifIFD directory with 27 entries]
  | | 0)  ExposureTime = 0.025 (10/400)
  | | 1)  FNumber = 2.8 (28/10)
  | | 2)  ExposureProgram = 2
  | | 3)  ISO = 160
  | | 4)  ExifVersion = 0220
  | | 5)  DateTimeOriginal = 2003:06:04 00:22:11
  | | 6)  CreateDate = 2003:06:04 00:22:11
  | | 7)  ComponentsConfiguration = 1 2 3 0
  | | 8)  CompressedBitsPerPixel = 4 (4/1)
  | | 9)  ExposureCompensation = 0 (0/10)
  | | 10) MaxApertureValue = 3 (48/16)
  | | 11) MeteringMode = 5
  | | 12) LightSource = 0
  | | 13) Flash = 15
  | | 14) FocalLength = 6 (60/10)
  | | 15) MakerNoteSony (SubDirectory) -->
  | | + [MakerNotes directory with 9 entries]
  | | | 0)  Sony_0x9001 = .....:....}....Q..........}@......7J0.}.J0[.}.J..^....^...}0[[snip]
  | | | 1)  Sony_0x9002 = ..........pp..Y.p..k@"}.].}...J.p.p.p.@
  | | | 2)  Sony_0x9003 = ..sF....<.....t.+R.u.u....q}W!G.G...(..0'................|...[snip]
  | | | 3)  Sony_0x9004 = ............
  | | | 4)  Sony_0x9005 = ..@U........}@..i.............@@@
  | | | 5)  Sony_0x9006 = ..{.?.b.E.X.K.../....V?.....l.G......}....W.X.o^...@.@.@^....[snip]
  | | | 6)  Sony_0x9007 = 9`...t...%\./G.....+...`'......\B.,..l>.....2Vb^j.(>..\~.... [snip]
  | | | 7)  Sony_0x9008 =
  | | | Warning = [minor] Bad format (61676) for MakerNotes entry 8

It looks like these tags about sony1 belong to MakerNotes.