Skip to content

str() on a Value aborts the process when charset conversion fails #65

Description

@titusz

Value.__str__ doesn't translate Exiv2::Error into a Python exception. If the value's text can't be converted to UTF-8, str() terminates the whole interpreter and there is no way to catch it from Python:

import exiv2

v = exiv2.CommentValue()
v.read(b"UNICODE\x00A")  # UserComment charset header + odd-length (invalid UTF-16) payload
str(v)
iconv: Invalid argument (errno = 22) inbytesleft = 1

after which the process aborts (exit 0xC0000409 on Windows, SIGABRT on macOS).

The same conversion through v.toString() raises a normal exiv2.Exiv2Error (kerInvalidIconvEncoding), which is what I'd expect from str() too.

This is reachable with ordinary files: some early-2000s cameras stored UCS-2 UserComments, e.g. https://commons.wikimedia.org/wiki/File:Bernhard_Richter.jpg (Kodak DX3500). Reading that file's metadata and calling str(datum.value()) aborts with "Cannot convert text encoding from 'UCS-2LE' to 'UTF-8'". We hit this in iscc-sdk through exactly that pattern and worked around it by switching to Metadatum.toString() (iscc/iscc-sdk#159). str() is also easy to call by accident via print(), logging or f-strings.

python-exiv2 0.18.1, libexiv2 0.28.8, Python 3.12, seen on Windows and macOS. The repro uses the deprecated bytes read() for brevity; values read from a real file behave the same.

Metadata

Metadata

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions