|
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 | + Py_XINCREF(item); |
| 56 | + return item; |
| 57 | +} |
| 58 | +#endif |
| 59 | + |
49 | 60 | extern PyObject *GitError; |
50 | 61 |
|
51 | 62 | extern PyTypeObject IndexType; |
@@ -599,8 +610,11 @@ merge_base_xxx(Repository *self, PyObject *args, git_merge_base_xxx_t git_merge_ |
599 | 610 | } |
600 | 611 |
|
601 | 612 | for (; i < commit_oid_count; i++) { |
602 | | - py_commit_oid = PyList_GET_ITEM(py_commit_oids, i); |
| 613 | + py_commit_oid = PyList_GetItemRef(py_commit_oids, i); |
| 614 | + if (py_commit_oid == NULL) |
| 615 | + goto out; |
603 | 616 | err = py_oid_to_git_oid_expand(self->repo, py_commit_oid, &commit_oids[i]); |
| 617 | + Py_DECREF(py_commit_oid); |
604 | 618 | if (err < 0) |
605 | 619 | goto out; |
606 | 620 | } |
@@ -1052,8 +1066,11 @@ Repository_create_commit(Repository *self, PyObject *args) |
1052 | 1066 | goto out; |
1053 | 1067 | } |
1054 | 1068 | for (; i < parent_count; i++) { |
1055 | | - py_parent = PyList_GET_ITEM(py_parents, i); |
| 1069 | + py_parent = PyList_GetItemRef(py_parents, i); |
| 1070 | + if (py_parent == NULL) |
| 1071 | + goto out; |
1056 | 1072 | len = py_oid_to_git_oid(py_parent, &oid); |
| 1073 | + Py_DECREF(py_parent); |
1057 | 1074 | if (len == 0) |
1058 | 1075 | goto out; |
1059 | 1076 | err = git_commit_lookup_prefix(&parents[i], self->repo, &oid, len); |
@@ -1135,8 +1152,11 @@ Repository_create_commit_string(Repository *self, PyObject *args) |
1135 | 1152 | goto out; |
1136 | 1153 | } |
1137 | 1154 | for (; i < parent_count; i++) { |
1138 | | - py_parent = PyList_GET_ITEM(py_parents, i); |
| 1155 | + py_parent = PyList_GetItemRef(py_parents, i); |
| 1156 | + if (py_parent == NULL) |
| 1157 | + goto out; |
1139 | 1158 | len = py_oid_to_git_oid(py_parent, &oid); |
| 1159 | + Py_DECREF(py_parent); |
1140 | 1160 | if (len == 0) |
1141 | 1161 | goto out; |
1142 | 1162 | err = git_commit_lookup_prefix(&parents[i], self->repo, &oid, len); |
|
0 commit comments