From 2adcf3f432b8d284ac2406e94549ada83253828a Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Fri, 15 Jan 2021 20:21:02 +0100 Subject: [PATCH] miniupnpcmodule.c: throw an exception in UPnP_discover() fixes #521 --- miniupnpc/miniupnpcmodule.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/miniupnpc/miniupnpcmodule.c b/miniupnpc/miniupnpcmodule.c index df44cf5..22be532 100644 --- a/miniupnpc/miniupnpcmodule.c +++ b/miniupnpc/miniupnpcmodule.c @@ -3,7 +3,7 @@ * Project : miniupnp * Author : Thomas BERNARD * website : https://miniupnp.tuxfamily.org/ - * copyright (c) 2007-2020 Thomas Bernard + * copyright (c) 2007-2021 Thomas Bernard * This software is subjet to the conditions detailed in the * provided LICENCE file. */ #include @@ -122,9 +122,9 @@ UPnPObject_dealloc(UPnPObject *self) static PyObject * UPnP_discover(UPnPObject *self) { - struct UPNPDev * dev; - int i; + int error = 0; PyObject *res = NULL; + if(self->devlist) { freeUPNPDevlist(self->devlist); @@ -137,13 +137,21 @@ UPnP_discover(UPnPObject *self) (int)self->localport, 0/*ip v6*/, 2/* TTL */, - 0/*error */); + &error); Py_END_ALLOW_THREADS /* Py_RETURN_NONE ??? */ - for(dev = self->devlist, i = 0; dev; dev = dev->pNext) - i++; - res = Py_BuildValue("i", i); - return res; + if (self->devlist != NULL) { + struct UPNPDev * dev; + int i = 0; + + for(dev = self->devlist; dev; dev = dev->pNext) + i++; + res = Py_BuildValue("i", i); + return res; + } else { + PyErr_SetString(PyExc_Exception, strupnperror(error)); + return NULL; + } } static PyObject *