[PATCH 2/2] [python] fix unsafe utf-8 decodings

Sebastian Spaeth Sebastian at SSpaeth.de
Wed Aug 17 05:48:58 PDT 2011


On Tue, 16 Aug 2011 22:37:47 +0100, Patrick Totzke <patricktotzke at googlemail.com> wrote:
> This prevents unsafe calls to decode for return
> value None in get_authors/get_subject

Thanks for the heads up, I just pushed a modified version of this. Some
comments on the code below.

Sebastian

> -        tag = Tags._get(self._tags).decode('utf-8')
> +        tag = Tags._get(self._tags)
> +        if tag:
> +            tag = tag.decode('UTF-8')

This was already safe as 
  if not nmlib.notmuch_tags_valid(self._tags):
was making sure that something useful will be returned.

> -        return Thread._get_authors(self._thread).decode('UTF-8')
> +        authors = Thread._get_authors(self._thread)
> +        if authors:
> +            return authors.decode('UTF-8')
> +        return None

> -        return Thread._get_subject(self._thread).decode('UTF-8')
> +        subject = Thread._get_subject(self._thread)
> +        if subject:
> +            return subject.decode('UTF-8')
> +        return None

Modified this to say:

foo = get_foo()
if foo is None:
   return None
return foo.decode('UTF-8')

Otherwise you would fall into a trap when e.g. the subject is empty and
a '' is returned. Your code would have returned "None". My version will
return ''.

Thanks!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://notmuchmail.org/pipermail/notmuch/attachments/20110817/2011a35f/attachment.pgp>


More information about the notmuch mailing list