Commit d311538a authored by Tim Peters's avatar Tim Peters

win32_urandom(): There's no need to copy the generated byte string, so

don't.
parent 9b279a8d
...@@ -7239,9 +7239,8 @@ static HCRYPTPROV hCryptProv = 0; ...@@ -7239,9 +7239,8 @@ static HCRYPTPROV hCryptProv = 0;
static PyObject* static PyObject*
win32_urandom(PyObject *self, PyObject *args) win32_urandom(PyObject *self, PyObject *args)
{ {
int howMany = 0; int howMany;
unsigned char* bytes = NULL; PyObject* result;
PyObject* returnVal = NULL;
/* Read arguments */ /* Read arguments */
if (! PyArg_ParseTuple(args, "i:urandom", &howMany)) if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
...@@ -7282,21 +7281,16 @@ win32_urandom(PyObject *self, PyObject *args) ...@@ -7282,21 +7281,16 @@ win32_urandom(PyObject *self, PyObject *args)
} }
/* Allocate bytes */ /* Allocate bytes */
bytes = (unsigned char*)PyMem_Malloc(howMany); result = PyString_FromStringAndSize(NULL, howMany);
if (bytes == NULL) if (result != NULL) {
return PyErr_NoMemory();
/* Get random data */ /* Get random data */
if (! pCryptGenRandom(hCryptProv, howMany, bytes)) { if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
PyMem_Free(bytes); PyString_AS_STRING(result))) {
Py_DECREF(result);
return win32_error("CryptGenRandom", NULL); return win32_error("CryptGenRandom", NULL);
} }
}
/* Build return value */ return result;
returnVal = PyString_FromStringAndSize(bytes, howMany);
PyMem_Free(bytes);
return returnVal;
} }
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment