Bug#902796: /usr/lib/python2.7/dist-packages/magic.py: Aborts; too many arguments to str()

2018-07-02 Thread Christoph Biedl
tags 902796 -moreinfo
thanks

Christoph Biedl wrote...

> This is certainly stable point release material, so if I can provide a
> fix within a very few days, this problem will go away quite soon.
> However, I'd like to see this with my own eyes first.

Okay, got a reproducer. This happens if the libmagic function returns
NULL (C) aka None (Python) - which isn't str (Python).

While I failed to trigger this using the buffer() and file() methods
without API misuse, see below, calling error() although there wasn't any
does the trick:

#!/usr/bin/python
import magic
ms = magic.open(magic.MAGIC_MIME)
ms.load()
print (ms.buffer(""))
print (ms.error())
ms.close()

Output:

| application/x-empty; charset=binary
| Traceback (most recent call last):
|   File "../example.py", line 6, in 
| print (ms.error())
|   File "/usr/lib/python2.7/dist-packages/magic.py", line 166, in error
| return str(e, 'utf-8')
| TypeError: str() takes at most 1 argument (2 given)

Fix is probably upstream commit FILE5_30-37-g8a942980, not tested yet.

Aside, the following code triggers an exception as well:

#!/usr/bin/python
import magic
ms = magic.open(magic.MAGIC_MIME)
#~ ms.load()
buf = ""
tp = ms.buffer(buf)
print (tp)
ms.close()

But skipping load() is a violation of the API since:

[magic.py, ll.155]

| def load(self, filename=None):
| """
+ Must be called to load entries in the colon separated list of database
+ files passed as argument or the default database file if no argument
+ before any magic queries can be performed.
| 
| Returns 0 on success and -1 on failure.
| """
| return _load(self._magic_t, filename)

And yes, people get this wrong, like in

Which is the only place I found where this message occured in the wild.

Christoph


signature.asc
Description: PGP signature


Bug#902796: /usr/lib/python2.7/dist-packages/magic.py: Aborts; too many arguments to str()

2018-07-01 Thread Christoph Biedl
tags 902796 moreinfo
fixed 902796 1:5.32-2
thanks

meba4815...@yahoo.com wrote...

> Traceback ended with:
> =
>   File "/usr/lib/python2.7/dist-packages/magic.py", line 155, in buffer
> return str(r, 'utf-8')
> TypeError: str() takes at most 1 argument (2 given)
> =

Can you please provide an example how you managed to trigger this? Also,
I'm a bit surprised nobody has managed to trigger this in years - still,
someone has to be first.

> The offending code seems gone from magic.py of the Testing package,
> but I did not test.

Confirmed, the python bindings have been removed recently.

> I classified this as important in part because I'm not a python
> programmer and this is my first bug report. For all I know, this could
> affect everyone (possibly depending on the data it's fed).

This is certainly stable point release material, so if I can provide a
fix within a very few days, this problem will go away quite soon.
However, I'd like to see this with my own eyes first.

Christoph


signature.asc
Description: PGP signature