Skip to content

Commit 6597495

Browse files
committed
refspec: borrow the C string
1 parent c8a4027 commit 6597495

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/refspec.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,16 @@ PyDoc_STRVAR(Refspec_src_matches__doc__,
105105
PyObject *
106106
Refspec_src_matches(Refspec *self, PyObject *py_str)
107107
{
108-
char *str;
108+
const char *str;
109+
PyObject *tstr;
109110
int res;
110111

111-
str = py_str_to_c_str(py_str, NULL);
112+
str = py_str_borrow_c_str(&tstr, py_str, NULL);
112113
if (!str)
113114
return NULL;
114115

115116
res = git_refspec_src_matches(self->refspec, str);
116-
free(str);
117+
Py_DECREF(tstr);
117118

118119
if (res)
119120
Py_RETURN_TRUE;
@@ -129,15 +130,16 @@ PyDoc_STRVAR(Refspec_dst_matches__doc__,
129130
PyObject *
130131
Refspec_dst_matches(Refspec *self, PyObject *py_str)
131132
{
132-
char *str;
133+
const char *str;
134+
PyObject *tstr;
133135
int res;
134136

135-
str = py_str_to_c_str(py_str, NULL);
137+
str = py_str_borrow_c_str(&tstr, py_str, NULL);
136138
if (!str)
137139
return NULL;
138140

139141
res = git_refspec_dst_matches(self->refspec, str);
140-
free(str);
142+
Py_DECREF(tstr);
141143

142144
if (res)
143145
Py_RETURN_TRUE;
@@ -153,24 +155,25 @@ PyDoc_STRVAR(Refspec_transform__doc__,
153155
PyObject *
154156
Refspec_transform(Refspec *self, PyObject *py_str)
155157
{
156-
char *str, *trans;
158+
const char *str;
159+
char *trans;
157160
int err, len, alen;
158-
PyObject *py_trans;
161+
PyObject *py_trans, *tstr;
159162

160-
str = py_str_to_c_str(py_str, NULL);
163+
str = py_str_borrow_c_str(&tstr, py_str, NULL);
161164
alen = len = strlen(str);
162165

163166
do {
164167
alen *= alen;
165168
trans = malloc(alen);
166169
if (!trans) {
167-
free(str);
170+
Py_DECREF(tstr);
168171
return PyErr_NoMemory();
169172
}
170173

171174
err = git_refspec_transform(trans, alen, self->refspec, str);
172175
} while(err == GIT_EBUFS);
173-
free(str);
176+
Py_DECREF(tstr);
174177

175178
if (err < 0) {
176179
free(trans);
@@ -193,24 +196,25 @@ PyDoc_STRVAR(Refspec_rtransform__doc__,
193196
PyObject *
194197
Refspec_rtransform(Refspec *self, PyObject *py_str)
195198
{
196-
char *str, *trans;
199+
const char *str;
200+
char *trans;
197201
int err, len, alen;
198-
PyObject *py_trans;
202+
PyObject *py_trans, *tstr;
199203

200-
str = py_str_to_c_str(py_str, NULL);
204+
str = py_str_borrow_c_str(&tstr, py_str, NULL);
201205
alen = len = strlen(str);
202206

203207
do {
204208
alen *= alen;
205209
trans = malloc(alen);
206210
if (!trans) {
207-
free(str);
211+
Py_DECREF(tstr);
208212
return PyErr_NoMemory();
209213
}
210214

211215
err = git_refspec_rtransform(trans, alen, self->refspec, str);
212216
} while(err == GIT_EBUFS);
213-
free(str);
217+
Py_DECREF(tstr);
214218

215219
if (err < 0) {
216220
free(trans);

0 commit comments

Comments
 (0)