Skip to content

Commit 0859ed8

Browse files
jkoenigertiran
andcommitted
fix(LDAPObject): Prevent memory errors in attrs_from_List
Function `PySequence_Length` can return -1 on iterables like `dict`. The following PyMem_NEW still succeeds due `PyMem_NEW(char *, -1 + 1)` being equivalent to `char** PyMem_Malloc(1)`, which then can result in a segmentation fault later on. Solution: Use `seq` and `PySequence_Size` to determine the size of the sequence. This way any iterable which contains only strings can be used. Co-authored-by: Christian Heimes <christian@python.org>
1 parent a1bdf47 commit 0859ed8

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Modules/LDAPObject.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,10 @@ attrs_from_List(PyObject *attrlist, char ***attrsp)
288288
if (seq == NULL)
289289
goto error;
290290

291-
len = PySequence_Length(attrlist);
291+
len = PySequence_Size(seq);
292+
if (len == -1) {
293+
goto error;
294+
}
292295

293296
attrs = PyMem_NEW(char *, len + 1);
294297

0 commit comments

Comments
 (0)