Skip to content

Commit e25e2f3

Browse files
besser82Daniel Zagaynov
authored andcommitted
pyxcrypt (_crypt_gensalt): Fix memory leak from not free'ed buffer.
1 parent 82cb8cb commit e25e2f3

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

src/pyxcrypt/pyxcrypt.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ static PyObject * _crypt_gensalt(PyObject *self, PyObject *args)
3636
errno = 0;
3737
int nrbytes;
3838
unsigned long count;
39-
char *_hash = NULL;
39+
char *setting = NULL;
4040
const char *prefix, *rbytes;
4141
Py_ssize_t *dumb_sz_1, *dumb_sz_2;
42+
PyObject *output = NULL;
4243

4344
if(PyTuple_Size(args) != 4)
4445
{
@@ -51,14 +52,21 @@ static PyObject * _crypt_gensalt(PyObject *self, PyObject *args)
5152
PyErr_SetString(PyExc_TypeError, "Arguments parsing");
5253
return NULL;
5354
}
54-
55-
if ((_hash = crypt_gensalt_ra(prefix, count, rbytes, nrbytes)) == NULL)
55+
setting = crypt_gensalt_ra(prefix, count, rbytes, nrbytes);
56+
if (setting == NULL)
5657
{
58+
if (errno == ENOMEM)
59+
{
60+
PyErr_SetString(PyExc_MemoryError, strerror(errno));
61+
return NULL;
62+
}
5763
PyErr_SetString(PyExc_RuntimeError, strerror(errno));
5864
return NULL;
5965
}
66+
output = Py_BuildValue("z", setting);
67+
free(setting);
6068

61-
return Py_BuildValue("z", _hash);
69+
return output;
6270
}
6371

6472
static PyObject * _crypt(PyObject *self, PyObject *args)

0 commit comments

Comments
 (0)