|  | 
| 46 | 46 | #include <git2/odb_backend.h> | 
| 47 | 47 | #include <git2/sys/repository.h> | 
| 48 | 48 | 
 | 
|  | 49 | +// TODO: remove this function when Python 3.13 becomes the minimum supported version | 
|  | 50 | +#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 13 | 
|  | 51 | +static inline PyObject * | 
|  | 52 | +PyList_GetItemRef(PyObject *op, Py_ssize_t index) | 
|  | 53 | +{ | 
|  | 54 | +    PyObject *item = PyList_GetItem(op, index); | 
|  | 55 | +    if (item != NULL) | 
|  | 56 | +        Py_IncRef(item); | 
|  | 57 | +    return item; | 
|  | 58 | +} | 
|  | 59 | +#endif | 
|  | 60 | + | 
| 49 | 61 | extern PyObject *GitError; | 
| 50 | 62 | 
 | 
| 51 | 63 | extern PyTypeObject IndexType; | 
| @@ -599,8 +611,11 @@ merge_base_xxx(Repository *self, PyObject *args, git_merge_base_xxx_t git_merge_ | 
| 599 | 611 |     } | 
| 600 | 612 | 
 | 
| 601 | 613 |     for (; i < commit_oid_count; i++) { | 
| 602 |  | -        py_commit_oid = PyList_GET_ITEM(py_commit_oids, i); | 
|  | 614 | +        py_commit_oid = PyList_GetItemRef(py_commit_oids, i); | 
|  | 615 | +        if (py_commit_oid == NULL) | 
|  | 616 | +            goto out; | 
| 603 | 617 |         err = py_oid_to_git_oid_expand(self->repo, py_commit_oid, &commit_oids[i]); | 
|  | 618 | +        Py_DecRef(py_commit_oid); | 
| 604 | 619 |         if (err < 0) | 
| 605 | 620 |             goto out; | 
| 606 | 621 |     } | 
| @@ -1052,8 +1067,11 @@ Repository_create_commit(Repository *self, PyObject *args) | 
| 1052 | 1067 |         goto out; | 
| 1053 | 1068 |     } | 
| 1054 | 1069 |     for (; i < parent_count; i++) { | 
| 1055 |  | -        py_parent = PyList_GET_ITEM(py_parents, i); | 
|  | 1070 | +        py_parent = PyList_GetItemRef(py_parents, i); | 
|  | 1071 | +        if (py_parent == NULL) | 
|  | 1072 | +            goto out; | 
| 1056 | 1073 |         len = py_oid_to_git_oid(py_parent, &oid); | 
|  | 1074 | +        Py_DecRef(py_parent); | 
| 1057 | 1075 |         if (len == 0) | 
| 1058 | 1076 |             goto out; | 
| 1059 | 1077 |         err = git_commit_lookup_prefix(&parents[i], self->repo, &oid, len); | 
| @@ -1135,8 +1153,11 @@ Repository_create_commit_string(Repository *self, PyObject *args) | 
| 1135 | 1153 |         goto out; | 
| 1136 | 1154 |     } | 
| 1137 | 1155 |     for (; i < parent_count; i++) { | 
| 1138 |  | -        py_parent = PyList_GET_ITEM(py_parents, i); | 
|  | 1156 | +        py_parent = PyList_GetItemRef(py_parents, i); | 
|  | 1157 | +        if (py_parent == NULL) | 
|  | 1158 | +            goto out; | 
| 1139 | 1159 |         len = py_oid_to_git_oid(py_parent, &oid); | 
|  | 1160 | +        Py_DecRef(py_parent); | 
| 1140 | 1161 |         if (len == 0) | 
| 1141 | 1162 |             goto out; | 
| 1142 | 1163 |         err = git_commit_lookup_prefix(&parents[i], self->repo, &oid, len); | 
|  | 
0 commit comments