mirror of
https://github.com/AuxXxilium/eudev.git
synced 2024-12-25 13:18:06 +07:00
python: fix error handling, and allocate argument array on the stack
This commit is contained in:
parent
0aee68ad02
commit
c4164442de
@ -21,6 +21,8 @@
|
|||||||
|
|
||||||
#include <Python.h>
|
#include <Python.h>
|
||||||
|
|
||||||
|
#include <alloca.h>
|
||||||
|
|
||||||
#define SD_JOURNAL_SUPPRESS_LOCATION
|
#define SD_JOURNAL_SUPPRESS_LOCATION
|
||||||
#include <systemd/sd-journal.h>
|
#include <systemd/sd-journal.h>
|
||||||
|
|
||||||
@ -36,20 +38,13 @@ static PyObject *journal_sendv(PyObject *self, PyObject *args) {
|
|||||||
PyObject *ret = NULL;
|
PyObject *ret = NULL;
|
||||||
PyObject **encoded;
|
PyObject **encoded;
|
||||||
|
|
||||||
|
/* Allocate an array for the argument strings */
|
||||||
argc = PyTuple_Size(args);
|
argc = PyTuple_Size(args);
|
||||||
|
encoded = alloca(argc * sizeof(PyObject*));
|
||||||
encoded = calloc(argc, sizeof(PyObject*));
|
memset(encoded, 0, argc * sizeof(PyObject*));
|
||||||
if (!encoded) {
|
|
||||||
ret = PyErr_NoMemory();
|
|
||||||
goto out1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Allocate sufficient iovector space for the arguments. */
|
/* Allocate sufficient iovector space for the arguments. */
|
||||||
iov = malloc(argc * sizeof(struct iovec));
|
iov = alloca(argc * sizeof(struct iovec));
|
||||||
if (!iov) {
|
|
||||||
ret = PyErr_NoMemory();
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Iterate through the Python arguments and fill the iovector. */
|
/* Iterate through the Python arguments and fill the iovector. */
|
||||||
for (i = 0; i < argc; ++i) {
|
for (i = 0; i < argc; ++i) {
|
||||||
@ -70,17 +65,11 @@ static PyObject *journal_sendv(PyObject *self, PyObject *args) {
|
|||||||
iov[i].iov_len = length;
|
iov[i].iov_len = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Clear errno, because sd_journal_sendv will not set it by
|
|
||||||
itself, unless an error occurs in one of the system calls. */
|
|
||||||
errno = 0;
|
|
||||||
|
|
||||||
/* Send the iovector to the journal. */
|
/* Send the iovector to the journal. */
|
||||||
r = sd_journal_sendv(iov, argc);
|
r = sd_journal_sendv(iov, argc);
|
||||||
if (r) {
|
if (r < 0) {
|
||||||
if (errno)
|
errno = -r;
|
||||||
PyErr_SetFromErrno(PyExc_IOError);
|
PyErr_SetFromErrno(PyExc_IOError);
|
||||||
else
|
|
||||||
PyErr_SetString(PyExc_ValueError, "invalid message format");
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,13 +81,6 @@ out:
|
|||||||
for (i = 0; i < argc; ++i)
|
for (i = 0; i < argc; ++i)
|
||||||
Py_XDECREF(encoded[i]);
|
Py_XDECREF(encoded[i]);
|
||||||
|
|
||||||
free(encoded);
|
|
||||||
|
|
||||||
out1:
|
|
||||||
/* Free the iovector. The actual strings
|
|
||||||
are already managed by Python. */
|
|
||||||
free(iov);
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,8 +99,10 @@ static PyObject* journal_stream_fd(PyObject *self, PyObject *args) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
fd = sd_journal_stream_fd(identifier, priority, level_prefix);
|
fd = sd_journal_stream_fd(identifier, priority, level_prefix);
|
||||||
if (fd < 0)
|
if (fd < 0) {
|
||||||
|
errno = -fd;
|
||||||
return PyErr_SetFromErrno(PyExc_IOError);
|
return PyErr_SetFromErrno(PyExc_IOError);
|
||||||
|
}
|
||||||
|
|
||||||
return PyLong_FromLong(fd);
|
return PyLong_FromLong(fd);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user