Skip to content

Commit c6a323c

Browse files
committed
reader: check return value of PySequence_Length
The constructor used to raise a MemoryError if files[0] returned an int, but files.__length__() did not exist.
1 parent b8c3714 commit c6a323c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/systemd/_reader.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ static int strv_converter(PyObject* obj, void *_result) {
107107
return 0;
108108

109109
Py_ssize_t len = PySequence_Length(obj);
110+
if (len < 0) {
111+
return 0;
112+
}
113+
110114
*result = new0(char*, len + 1);
111115
if (!*result) {
112116
set_error(-ENOMEM, NULL, NULL);
@@ -166,6 +170,10 @@ static int intlist_converter(PyObject* obj, int **_result, size_t *_len) {
166170
return 0;
167171

168172
Py_ssize_t len = PySequence_Length(obj);
173+
if (len < 0) {
174+
return 0;
175+
}
176+
169177
_cleanup_free_ int *result = new0(int, len);
170178

171179
if (!result) {

src/systemd/test/test_reader.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,15 @@
44

55
import pytest
66

7+
class SequenceWithoutLength:
8+
def __getitem__(self, index: int) -> int:
9+
return 5
10+
11+
# no __length__ implementation
12+
713
def test_reader_init_with_empty_files_sequence():
814
_Reader(files=[], flags=0)
15+
16+
def test_reader_init_with_sequence_without_length():
17+
with pytest.raises(TypeError):
18+
_Reader(files=SequenceWithoutLength(), flags=0)

0 commit comments

Comments
 (0)