Skip to content

Commit ffba3a2

Browse files
committed
journal: check errors properly in query_unique
1 parent 8583a45 commit ffba3a2

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

systemd/_reader.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,8 @@ static PyObject* Reader_query_unique(Reader *self, PyObject *args) {
832832
int r;
833833
const void *uniq;
834834
size_t uniq_len;
835-
PyObject *value_set, *key, *value;
835+
_cleanup_Py_DECREF_ PyObject *_value_set = NULL, *key = NULL;
836+
PyObject *value_set;
836837

837838
if (!PyArg_ParseTuple(args, "s:query_unique", &query))
838839
return NULL;
@@ -844,21 +845,35 @@ static PyObject* Reader_query_unique(Reader *self, PyObject *args) {
844845
if (set_error(r, NULL, "Invalid field name") < 0)
845846
return NULL;
846847

847-
value_set = PySet_New(0);
848+
value_set = _value_set = PySet_New(0);
849+
if (!value_set)
850+
return NULL;
851+
848852
key = unicode_FromString(query);
853+
if (!key)
854+
return NULL;
849855

850856
SD_JOURNAL_FOREACH_UNIQUE(self->j, uniq, uniq_len) {
851857
const char *delim_ptr;
858+
_cleanup_Py_DECREF_ PyObject *value = NULL;
852859

853860
delim_ptr = memchr(uniq, '=', uniq_len);
861+
if (!delim_ptr) {
862+
set_error(-EINVAL, NULL, "Invalid field in the journal");
863+
return NULL;
864+
}
865+
854866
value = PyBytes_FromStringAndSize(
855867
delim_ptr + 1,
856868
(const char*) uniq + uniq_len - (delim_ptr + 1));
857-
PySet_Add(value_set, value);
858-
Py_DECREF(value);
869+
if (!value)
870+
return NULL;
871+
872+
if (PySet_Add(value_set, value) < 0)
873+
return NULL;
859874
}
860875

861-
Py_DECREF(key);
876+
_value_set = NULL;
862877
return value_set;
863878
}
864879

0 commit comments

Comments
 (0)