Skip to content

Commit deaa4e4

Browse files
committed
fix memory leak in ioctl_capabilities()
1 parent 575c399 commit deaa4e4

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

evdev/device.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def __init__(self, dev):
9393
self.fd = os.open(dev, os.O_RDWR | os.O_NONBLOCK)
9494

9595
# Returns (bustype, vendor, product, version, name, phys, capabilities)
96-
info_res = _input.ioctl_devinfo(self.fd)
96+
info_res = _input.ioctl_devinfo(self.fd)
9797

9898
#: A :class:`DeviceInfo <evdev.device.DeviceInfo>` instance
9999
self.info = DeviceInfo(*info_res[:4])
@@ -108,7 +108,7 @@ def __init__(self, dev):
108108
self.version = _input.ioctl_EVIOCGVERSION(self.fd)
109109

110110
#: The raw dictionary of device capabilities - see `:func:capabilities()`
111-
self._rawcapabilities = info_res[6]
111+
self._rawcapabilities = _input.ioctl_capabilities(self.fd)
112112

113113
def _capabilities(self, absinfo=True):
114114
res = {}

evdev/input.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ ioctl_capabilities(PyObject *self, PyObject *args)
147147
// events e.g: {1: [272, 273, 274, 275], 2: [0, 1, 6, 8]}
148148
PyObject* capabilities = PyDict_New();
149149
PyObject* eventcodes = NULL;
150+
PyObject* evlong = NULL;
150151
PyObject* capability = NULL;
151152
PyObject* py_absinfo = NULL;
152153
PyObject* absitem = NULL;
@@ -181,23 +182,33 @@ ioctl_capabilities(PyObject *self, PyObject *args)
181182
absinfo.flat,
182183
absinfo.resolution);
183184

184-
absitem = Py_BuildValue("(OO)", PyLong_FromLong(ev_code), py_absinfo);
185+
evlong = PyLong_FromLong(ev_code);
186+
absitem = Py_BuildValue("(OO)", evlong, py_absinfo);
185187

186188
// absitem -> tuple(ABS_X, (0, 255, 0, 0))
187189
PyList_Append(eventcodes, absitem);
190+
191+
Py_DECREF(absitem);
192+
Py_DECREF(py_absinfo);
188193
}
189194
else {
190-
PyList_Append(eventcodes, PyLong_FromLong(ev_code));
195+
evlong = PyLong_FromLong(ev_code);
196+
PyList_Append(eventcodes, evlong);
191197
}
198+
199+
Py_DECREF(evlong);
192200
}
193201
}
194202
// capabilities[EV_KEY] = [KEY_A, KEY_B, KEY_C, ...]
195203
// capabilities[EV_ABS] = [(ABS_X, (0, 255, 0, 0)), ...]
196204
PyDict_SetItem(capabilities, capability, eventcodes);
205+
206+
Py_DECREF(capability);
207+
Py_DECREF(eventcodes);
197208
}
198209
}
199210

200-
return Py_BuildValue("O", capabilities);
211+
return capabilities;
201212

202213
on_err:
203214
PyErr_SetFromErrno(PyExc_IOError);
@@ -210,7 +221,6 @@ static PyObject *
210221
ioctl_devinfo(PyObject *self, PyObject *args)
211222
{
212223
int fd;
213-
PyObject* capabilities = NULL;
214224

215225
struct input_id iid;
216226
char name[MAX_NAME_SIZE];
@@ -224,14 +234,11 @@ ioctl_devinfo(PyObject *self, PyObject *args)
224234
if (ioctl(fd, EVIOCGID, &iid) < 0) goto on_err;
225235
if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0) goto on_err;
226236

227-
// Get device capabilities
228-
capabilities = ioctl_capabilities(self, Py_BuildValue("(i)", fd));
229-
230237
// Some devices do not have a physical topology associated with them
231238
ioctl(fd, EVIOCGPHYS(sizeof(phys)), phys);
232239

233-
return Py_BuildValue("hhhhssO", iid.bustype, iid.vendor, iid.product, iid.version,
234-
name, phys, capabilities);
240+
return Py_BuildValue("hhhhss", iid.bustype, iid.vendor, iid.product, iid.version,
241+
name, phys);
235242

236243
on_err:
237244
PyErr_SetFromErrno(PyExc_IOError);

0 commit comments

Comments
 (0)