Skip to content

Commit 7bea492

Browse files
committed
Don't decref PyBytesObject before formatting an error with it
1 parent 584bda3 commit 7bea492

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

src/pygit2.c

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ discover_repository(PyObject *self, PyObject *args)
109109
const char *ceiling_dirs = NULL;
110110
PyObject *py_repo_path = NULL;
111111
int err;
112+
PyObject *result = NULL;
112113

113114
if (!PyArg_ParseTuple(args, "O&|IO&", PyUnicode_FSConverter, &py_path, &across_fs,
114115
PyUnicode_FSConverter, &py_ceiling_dirs))
@@ -122,18 +123,20 @@ discover_repository(PyObject *self, PyObject *args)
122123
memset(&repo_path, 0, sizeof(git_buf));
123124
err = git_repository_discover(&repo_path, path, across_fs, ceiling_dirs);
124125

125-
Py_XDECREF(py_path);
126-
Py_XDECREF(py_ceiling_dirs);
127-
128-
if (err == GIT_ENOTFOUND)
129-
Py_RETURN_NONE;
130-
if (err < 0)
131-
return Error_set_str(err, path);
126+
if (err == GIT_OK) {
127+
py_repo_path = PyUnicode_DecodeFSDefault(repo_path.ptr);
128+
git_buf_dispose(&repo_path);
129+
result = py_repo_path;
130+
} else if (err == GIT_ENOTFOUND) {
131+
result = Py_None;
132+
} else {
133+
result = Error_set_str(err, path);
134+
}
132135

133-
py_repo_path = PyUnicode_DecodeFSDefault(repo_path.ptr);
134-
git_buf_dispose(&repo_path);
136+
Py_XDECREF(py_ceiling_dirs);
137+
Py_XDECREF(py_path);
135138

136-
return py_repo_path;
139+
return result;
137140
};
138141

139142
PyDoc_STRVAR(hashfile__doc__,
@@ -200,6 +203,7 @@ init_file_backend(PyObject *self, PyObject *args)
200203
unsigned int flags = 0;
201204
int err = GIT_OK;
202205
git_repository *repository = NULL;
206+
PyObject *result = NULL;
203207

204208
if (!PyArg_ParseTuple(args, "O&|I", PyUnicode_FSConverter, &py_path, &flags))
205209
return NULL;
@@ -208,25 +212,23 @@ init_file_backend(PyObject *self, PyObject *args)
208212

209213
err = git_repository_open_ext(&repository, path, flags, NULL);
210214

211-
Py_XDECREF(py_path);
212-
213-
if (err < 0) {
215+
if (err == GIT_OK) {
216+
result = PyCapsule_New(repository, "backend", NULL);
217+
} else {
218+
result = NULL;
214219
Error_set_str(err, path);
215-
goto cleanup;
216-
}
217-
218-
return PyCapsule_New(repository, "backend", NULL);
219220

220-
cleanup:
221-
if (repository) {
222-
git_repository_free(repository);
223-
}
221+
if (repository) {
222+
git_repository_free(repository);
223+
}
224224

225-
if (err == GIT_ENOTFOUND) {
226-
PyErr_Format(GitError, "Repository not found at %s", path);
225+
if (err == GIT_ENOTFOUND) {
226+
PyErr_Format(GitError, "Repository not found at %s", path);
227+
}
227228
}
228229

229-
return NULL;
230+
Py_XDECREF(py_path);
231+
return result;
230232
}
231233

232234

0 commit comments

Comments
 (0)