All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH libgpiod] bindings: python: fix Line.request() crashing
@ 2020-10-06 17:17 Jiri Benc
  2020-10-09  7:40 ` Bartosz Golaszewski
  0 siblings, 1 reply; 4+ messages in thread
From: Jiri Benc @ 2020-10-06 17:17 UTC (permalink / raw)
  To: linux-gpio

On an attempt to call the 'request' method of a Line object, the program
crashes with this exception:

> SystemError: ../Objects/dictobject.c:2606: bad argument to internal function
>
> The above exception was the direct cause of the following exception:
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> SystemError: <class 'gpiod.LineBulk'> returned a result with an error set

The problem is that keyword args are NULL (rather than an empty dict) if
they are not present. However, PyDict_Size sets an exception if it gets
NULL.

Fixes: 02a3d0a2ab5e ("bindings: python: fix segfault when calling Line.request()")
Signed-off-by: Jiri Benc <jbenc@upir.cz>
---
 bindings/python/gpiodmodule.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bindings/python/gpiodmodule.c b/bindings/python/gpiodmodule.c
index b3ae2bfebb8a..fee4c32406fa 100644
--- a/bindings/python/gpiodmodule.c
+++ b/bindings/python/gpiodmodule.c
@@ -472,7 +472,7 @@ static PyObject *gpiod_Line_request(gpiod_LineObject *self,
 	gpiod_LineBulkObject *bulk_obj;
 	int rv;
 
-	if (PyDict_Size(kwds) > 0) {
+	if (kwds && PyDict_Size(kwds) > 0) {
 		def_val = PyDict_GetItemString(kwds, "default_val");
 		def_vals = PyDict_GetItemString(kwds, "default_vals");
 	} else {
-- 
2.18.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH libgpiod] bindings: python: fix Line.request() crashing
  2020-10-06 17:17 [PATCH libgpiod] bindings: python: fix Line.request() crashing Jiri Benc
@ 2020-10-09  7:40 ` Bartosz Golaszewski
  2020-10-09  9:19   ` Jiri Benc
  0 siblings, 1 reply; 4+ messages in thread
From: Bartosz Golaszewski @ 2020-10-09  7:40 UTC (permalink / raw)
  To: Jiri Benc; +Cc: open list:GPIO SUBSYSTEM

On Tue, Oct 6, 2020 at 7:26 PM Jiri Benc <jbenc@upir.cz> wrote:
>
> On an attempt to call the 'request' method of a Line object, the program
> crashes with this exception:
>
> > SystemError: ../Objects/dictobject.c:2606: bad argument to internal function
> >
> > The above exception was the direct cause of the following exception:
> >
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > SystemError: <class 'gpiod.LineBulk'> returned a result with an error set
>
> The problem is that keyword args are NULL (rather than an empty dict) if
> they are not present. However, PyDict_Size sets an exception if it gets
> NULL.
>
> Fixes: 02a3d0a2ab5e ("bindings: python: fix segfault when calling Line.request()")
> Signed-off-by: Jiri Benc <jbenc@upir.cz>
> ---
>  bindings/python/gpiodmodule.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/bindings/python/gpiodmodule.c b/bindings/python/gpiodmodule.c
> index b3ae2bfebb8a..fee4c32406fa 100644
> --- a/bindings/python/gpiodmodule.c
> +++ b/bindings/python/gpiodmodule.c
> @@ -472,7 +472,7 @@ static PyObject *gpiod_Line_request(gpiod_LineObject *self,
>         gpiod_LineBulkObject *bulk_obj;
>         int rv;
>
> -       if (PyDict_Size(kwds) > 0) {
> +       if (kwds && PyDict_Size(kwds) > 0) {
>                 def_val = PyDict_GetItemString(kwds, "default_val");
>                 def_vals = PyDict_GetItemString(kwds, "default_vals");
>         } else {
> --
> 2.18.1
>

Hi Jiri!

Thanks for the fixes. Could you send me a chunk of code that triggers
this error so I can make a test-case for it?

Bartosz

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH libgpiod] bindings: python: fix Line.request() crashing
  2020-10-09  7:40 ` Bartosz Golaszewski
@ 2020-10-09  9:19   ` Jiri Benc
  2020-10-09 12:37     ` Bartosz Golaszewski
  0 siblings, 1 reply; 4+ messages in thread
From: Jiri Benc @ 2020-10-09  9:19 UTC (permalink / raw)
  To: Bartosz Golaszewski; +Cc: open list:GPIO SUBSYSTEM

On Fri, 9 Oct 2020 09:40:29 +0200, Bartosz Golaszewski wrote:
> Thanks for the fixes. Could you send me a chunk of code that triggers
> this error so I can make a test-case for it?

It was as simple as this:

import gpiod
c = gpiod.Chip('0')
l = c.get_line(17)
l.request('my')

Latest stable Raspbian (Debian 10.6), Python 3.7.3. It's so basic that
it's puzzling me that nobody has hit it before me. But my patch is
right, see https://docs.python.org/3/c-api/structures.html:

------
METH_VARARGS | METH_KEYWORDS

Methods with these flags must be of type PyCFunctionWithKeywords. The
function expects three parameters: self, args, kwargs where kwargs is a
dictionary of all the keyword arguments or possibly NULL if there are
no keyword arguments.
------

Yet, commit 02a3d0a2ab5e that attempted to fix this states that the
kwds dictionary was an empty dict. Maybe a change in Python C API in
3.7? The Python 3.6 and earlier documentation did not mention the NULL.
I can't find anything in Python release notes, though.

 Jiri

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH libgpiod] bindings: python: fix Line.request() crashing
  2020-10-09  9:19   ` Jiri Benc
@ 2020-10-09 12:37     ` Bartosz Golaszewski
  0 siblings, 0 replies; 4+ messages in thread
From: Bartosz Golaszewski @ 2020-10-09 12:37 UTC (permalink / raw)
  To: Jiri Benc; +Cc: open list:GPIO SUBSYSTEM

On Fri, Oct 9, 2020 at 11:20 AM Jiri Benc <jbenc@upir.cz> wrote:
>
> On Fri, 9 Oct 2020 09:40:29 +0200, Bartosz Golaszewski wrote:
> > Thanks for the fixes. Could you send me a chunk of code that triggers
> > this error so I can make a test-case for it?
>
> It was as simple as this:
>
> import gpiod
> c = gpiod.Chip('0')
> l = c.get_line(17)
> l.request('my')
>
> Latest stable Raspbian (Debian 10.6), Python 3.7.3. It's so basic that
> it's puzzling me that nobody has hit it before me. But my patch is
> right, see https://docs.python.org/3/c-api/structures.html:
>
> ------
> METH_VARARGS | METH_KEYWORDS
>
> Methods with these flags must be of type PyCFunctionWithKeywords. The
> function expects three parameters: self, args, kwargs where kwargs is a
> dictionary of all the keyword arguments or possibly NULL if there are
> no keyword arguments.
> ------
>
> Yet, commit 02a3d0a2ab5e that attempted to fix this states that the
> kwds dictionary was an empty dict. Maybe a change in Python C API in
> 3.7? The Python 3.6 and earlier documentation did not mention the NULL.
> I can't find anything in Python release notes, though.
>
>  Jiri

Thanks. I added a test-case for this and applied your patch to master
and stable v1.6 & 1.4.

Bartosz

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-10-09 12:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-06 17:17 [PATCH libgpiod] bindings: python: fix Line.request() crashing Jiri Benc
2020-10-09  7:40 ` Bartosz Golaszewski
2020-10-09  9:19   ` Jiri Benc
2020-10-09 12:37     ` Bartosz Golaszewski

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.