A ValueError is raised when accessing the value of a malformed MakerNote

Bug #942595 reported by Hobson Lane
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
pyexiv2
Fix Committed
Medium
Unassigned

Bug Description

There are TODOs in the source code to address this bug and crude work-around patch is provided.

If a tag value contains a single Null byte ( chr(0) ), it is interpreted by pyexiv2 as an "undefined" value, and attempting to read the value for that tag produces the error

    ValueError: invalid literal for int() with base 10: ''

 when pyexiv2 attempts to convert the string to an integer. This error only occured for a Canon EOS image that was processed by the Windows photo manager and then had its EXIF tags read and re-written by pyexiv2 in Ubuntu. Sample images will be provided in comments to this bug.

known to be present in version 0.3.0 (Ubuntu distribution package) and version 0.3.2 (development) of pyexiv2

Related branches

Revision history for this message
Hobson Lane (hobs) wrote :
Revision history for this message
Hobson Lane (hobs) wrote :

  File "/usr/lib/python2.7/dist-packages/pyexiv2/exif.py", line 190, in _get_value
    self._compute_value()
  File "/usr/lib/python2.7/dist-packages/pyexiv2/exif.py", line 185, in _compute_value
    self._value = self._convert_to_python(self._raw_value)
  File "/usr/lib/python2.7/dist-packages/pyexiv2/exif.py", line 333, in _convert_to_python
    return undefined_to_string(value)
  File "/usr/lib/python2.7/dist-packages/pyexiv2/utils.py", line 148, in undefined_to_string
    return ''.join(map(lambda x: chr(int(x)), undefined.rstrip().split(' ')))
  File "/usr/lib/python2.7/dist-packages/pyexiv2/utils.py", line 148, in <lambda>
    return ''.join(map(lambda x: chr(int(x)), undefined.rstrip().split(' ')))
ValueError: invalid literal for int() with base 10: ''

Revision history for this message
Hobson Lane (hobs) wrote :

Better fix.
Implements latin encoding translation to ascii (required by python) before using exif.undefined_to_string() on tag strings.
Also escapes nonprintable characters using backslashes like \x00 for NULL or EOF.

Revision history for this message
Olivier Tilloy (osomon) wrote :

I can reproduce with the picture attached in comment #2 and the following script:

    import pyexiv2
    m = pyexiv2.ImageMetadata('IMG_5033.JPG')
    m.read()
    print m['Exif.Photo.MakerNote'].value

summary: - A malformed MakerNote crashes pyexiv2
+ A ValueError is raised when accessing the value of a malformed MakerNote
Changed in pyexiv2:
importance: Low → Medium
status: New → Confirmed
Revision history for this message
Olivier Tilloy (osomon) wrote :

Note that the raw_value attribute of the tag (m['Exif.Photo.MakerNote']) is an empty string, and this is what causes the ValueError to be raised: utils.undefined_to_string(…) expects its parameter to contain the ascii codes of a sequence of bytes, separated by white spaces (e.g. "48 50 50 49").

It would be relatively easy to patch utils.undefined_to_string(…) to check for an empty string and return an empty string in that case.

Hobson, wouldn’t that be enough to fix the bug? As I understand it, your patch tries to address a more general problem. I’d rather fix this specific one, and then address other issues separately, testing against actual images that exhibit the problem and writing unit tests to ensure no regressions.

Revision history for this message
Hobson Lane (hobs) wrote : Re: [Bug 942595] Re: A ValueError is raised when accessing the value of a malformed MakerNote

Sounds good. It makes sense to be conservative with the general release
branch. The more general patch is probably only useful for me. I like to
dump the raw bytes of tags like MakerNote in a semi-human-readable form,
even when they are "undefined."
--Hobson

On Fri, Mar 9, 2012 at 6:40 PM, Olivier Tilloy <email address hidden> wrote:

> Note that the raw_value attribute of the tag (m['Exif.Photo.MakerNote'])
> is an empty string, and this is what causes the ValueError to be raised:
> utils.undefined_to_string(…) expects its parameter to contain the ascii
> codes of a sequence of bytes, separated by white spaces (e.g. "48 50 50
> 49").
>
> It would be relatively easy to patch utils.undefined_to_string(…) to
> check for an empty string and return an empty string in that case.
>
> Hobson, wouldn’t that be enough to fix the bug? As I understand it, your
> patch tries to address a more general problem. I’d rather fix this
> specific one, and then address other issues separately, testing against
> actual images that exhibit the problem and writing unit tests to ensure
> no regressions.
>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/942595
>
> Title:
> A ValueError is raised when accessing the value of a malformed
> MakerNote
>
> Status in pyexiv2, a python binding to exiv2:
> Confirmed
>
> Bug description:
> There are TODOs in the source code to address this bug and crude
> work-around patch is provided.
>
> If a tag value contains a single Null byte ( chr(0) ), it is
> interpreted by pyexiv2 as an "undefined" value, and attempting to read
> the value for that tag produces the error
>
> ValueError: invalid literal for int() with base 10: ''
>
> when pyexiv2 attempts to convert the string to an integer. This error
> only occured for a Canon EOS image that was processed by the Windows
> photo manager and then had its EXIF tags read and re-written by
> pyexiv2 in Ubuntu. Sample images will be provided in comments to this
> bug.
>
> known to be present in version 0.3.0 (Ubuntu distribution package) and
> version 0.3.2 (development) of pyexiv2
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/pyexiv2/+bug/942595/+subscriptions
>

Revision history for this message
Olivier Tilloy (osomon) wrote :

> I like to dump the raw bytes of tags like MakerNote in a semi-human-readable
> form, even when they are "undefined."

Sounds like this would be a good candidate for a helper function in the utils module.
If you want to submit it as a separate patch, I’ll be happy to review and merge it.

Revision history for this message
Olivier Tilloy (osomon) wrote :

Fixed at revision 374 of the trunk.

Changed in pyexiv2:
assignee: nobody → Olivier Tilloy (osomon)
status: Confirmed → Fix Committed
Revision history for this message
Hobson Lane (hobs) wrote :

Will do. This weekend.

On Thu, Mar 15, 2012 at 5:21 PM, Olivier Tilloy <email address hidden> wrote:

> > I like to dump the raw bytes of tags like MakerNote in a
> semi-human-readable
> > form, even when they are "undefined."
>
> Sounds like this would be a good candidate for a helper function in the
> utils module.
> If you want to submit it as a separate patch, I’ll be happy to review and
> merge it.
>
> --
> You received this bug notification because you are subscribed to the bug
> report.
> https://bugs.launchpad.net/bugs/942595
>
> Title:
> A ValueError is raised when accessing the value of a malformed
> MakerNote
>
> Status in pyexiv2, a python binding to exiv2:
> Confirmed
>
> Bug description:
> There are TODOs in the source code to address this bug and crude
> work-around patch is provided.
>
> If a tag value contains a single Null byte ( chr(0) ), it is
> interpreted by pyexiv2 as an "undefined" value, and attempting to read
> the value for that tag produces the error
>
> ValueError: invalid literal for int() with base 10: ''
>
> when pyexiv2 attempts to convert the string to an integer. This error
> only occured for a Canon EOS image that was processed by the Windows
> photo manager and then had its EXIF tags read and re-written by
> pyexiv2 in Ubuntu. Sample images will be provided in comments to this
> bug.
>
> known to be present in version 0.3.0 (Ubuntu distribution package) and
> version 0.3.2 (development) of pyexiv2
>
> To manage notifications about this bug go to:
> https://bugs.launchpad.net/pyexiv2/+bug/942595/+subscriptions
>

Olivier Tilloy (osomon)
Changed in pyexiv2:
assignee: Olivier Tilloy (osomon) → nobody
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.