Skip to content

Commit 5227b16

Browse files
committed
Fix memory leaks
1 parent a5d8cf0 commit 5227b16

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

src/evdev/input.c

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ device_read(PyObject *self, PyObject *args)
6363
return NULL;
6464
}
6565

66-
PyObject* sec = PyLong_FromLong(event.input_event_sec);
67-
PyObject* usec = PyLong_FromLong(event.input_event_usec);
68-
PyObject* val = PyLong_FromLong(event.value);
69-
PyObject* type = PyLong_FromLong(event.type);
70-
PyObject* code = PyLong_FromLong(event.code);
71-
PyObject* py_input_event = PyTuple_Pack(5, sec, usec, type, code, val);
66+
PyObject *py_input_event = PyTuple_New(5);
67+
PyTuple_SET_ITEM(py_input_event, 0, PyLong_FromLong(event.input_event_sec));
68+
PyTuple_SET_ITEM(py_input_event, 1, PyLong_FromLong(event.input_event_usec));
69+
PyTuple_SET_ITEM(py_input_event, 2, PyLong_FromLong(event.type));
70+
PyTuple_SET_ITEM(py_input_event, 3, PyLong_FromLong(event.code));
71+
PyTuple_SET_ITEM(py_input_event, 4, PyLong_FromLong(event.value));
7272

7373
return py_input_event;
7474
}
@@ -81,14 +81,6 @@ device_read_many(PyObject *self, PyObject *args)
8181
// get device file descriptor (O_RDONLY|O_NONBLOCK)
8282
int fd = (int)PyLong_AsLong(PyTuple_GET_ITEM(args, 0));
8383

84-
PyObject* py_input_event = NULL;
85-
PyObject* events = NULL;
86-
PyObject* sec = NULL;
87-
PyObject* usec = NULL;
88-
PyObject* val = NULL;
89-
PyObject* type = NULL;
90-
PyObject* code = NULL;
91-
9284
struct input_event event[64];
9385

9486
size_t event_size = sizeof(struct input_event);
@@ -101,15 +93,15 @@ device_read_many(PyObject *self, PyObject *args)
10193

10294
// Construct a tuple of event tuples. Each tuple is the arguments to InputEvent.
10395
size_t num_events = nread / event_size;
104-
events = PyTuple_New(num_events);
105-
for (size_t i = 0 ; i < num_events; i++) {
106-
sec = PyLong_FromLong(event[i].input_event_sec);
107-
usec = PyLong_FromLong(event[i].input_event_usec);
108-
val = PyLong_FromLong(event[i].value);
109-
type = PyLong_FromLong(event[i].type);
110-
code = PyLong_FromLong(event[i].code);
11196

112-
py_input_event = PyTuple_Pack(5, sec, usec, type, code, val);
97+
PyObject* events = PyTuple_New(num_events);
98+
for (size_t i = 0 ; i < num_events; i++) {
99+
PyObject *py_input_event = PyTuple_New(5);
100+
PyTuple_SET_ITEM(py_input_event, 0, PyLong_FromLong(event[i].input_event_sec));
101+
PyTuple_SET_ITEM(py_input_event, 1, PyLong_FromLong(event[i].input_event_usec));
102+
PyTuple_SET_ITEM(py_input_event, 2, PyLong_FromLong(event[i].type));
103+
PyTuple_SET_ITEM(py_input_event, 3, PyLong_FromLong(event[i].code));
104+
PyTuple_SET_ITEM(py_input_event, 4, PyLong_FromLong(event[i].value));
113105
PyTuple_SET_ITEM(events, i, py_input_event);
114106
}
115107

@@ -200,6 +192,11 @@ ioctl_capabilities(PyObject *self, PyObject *args)
200192
return capabilities;
201193

202194
on_err:
195+
Py_XDECREF(capabilities);
196+
Py_XDECREF(eventcodes);
197+
Py_XDECREF(capability);
198+
Py_XDECREF(py_absinfo);
199+
Py_XDECREF(absitem);
203200
PyErr_SetFromErrno(PyExc_OSError);
204201
return NULL;
205202
}
@@ -408,7 +405,9 @@ ioctl_EVIOCG_bits(PyObject *self, PyObject *args)
408405
PyObject* res = PyList_New(0);
409406
for (int i=0; i<=max; i++) {
410407
if (test_bit(bytes, i)) {
411-
PyList_Append(res, Py_BuildValue("i", i));
408+
PyObject *val = PyLong_FromLong(i);
409+
PyList_Append(res, val);
410+
Py_DECREF(val);
412411
}
413412
}
414413

@@ -523,7 +522,9 @@ ioctl_EVIOCGPROP(PyObject *self, PyObject *args)
523522
PyObject* res = PyList_New(0);
524523
for (int i=0; i<INPUT_PROP_MAX; i++) {
525524
if (test_bit(bytes, i)) {
526-
PyList_Append(res, Py_BuildValue("i", i));
525+
PyObject *val = PyLong_FromLong(i);
526+
PyList_Append(res, val);
527+
Py_DECREF(val);
527528
}
528529
}
529530

0 commit comments

Comments
 (0)