Skip to content

Commit 014b776

Browse files
besser82Daniel Zagaynov
authored andcommitted
pyxcrypt (_crypt): Remove unneeded memcpy operation.
Also call crypt_ra(3) to do the allocation of struct crypt_data.
1 parent e25e2f3 commit 014b776

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

src/pyxcrypt/pyxcrypt.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ static PyObject * _crypt(PyObject *self, PyObject *args)
7373
{
7474
errno = 0;
7575
const char *phrase, *setting;
76-
char hash[CRYPT_GENSALT_OUTPUT_SIZE];
76+
char *hash = NULL;
7777
struct crypt_data *data = NULL;
78+
int size = 0;
7879
Py_ssize_t *dumb_sz_1, *dumb_sz_2;
80+
PyObject *output = NULL;
7981

8082
if(PyTuple_Size(args) != 2)
8183
{
@@ -88,23 +90,22 @@ static PyObject * _crypt(PyObject *self, PyObject *args)
8890
PyErr_SetString(PyExc_TypeError, "Arguments parsing");
8991
return NULL;
9092
}
91-
if ((data = malloc(sizeof(*data))) == NULL)
93+
hash = crypt_ra(phrase, setting, (void **)&data, &size);
94+
if (data == NULL)
9295
{
9396
PyErr_SetString(PyExc_MemoryError, strerror(errno));
9497
return NULL;
9598
}
96-
memset(data, 0, sizeof(*data));
97-
if (crypt_r(phrase, setting, data) == NULL || data->output[0] == '*')
99+
if (hash == NULL)
98100
{
99101
PyErr_SetString(PyExc_RuntimeError, strerror(errno));
100102
free(data);
101103
return NULL;
102104
}
103-
104-
memcpy(hash, data->output, CRYPT_GENSALT_OUTPUT_SIZE);
105+
output = Py_BuildValue("z", hash);
105106
free(data);
106107

107-
return Py_BuildValue("z", hash);
108+
return output;
108109
}
109110

110111
static PyObject * _crypt_checksalt(PyObject *self, PyObject *args)

0 commit comments

Comments
 (0)