Skip to content

Commit 15a4f2f

Browse files
committed
refactor alloc() logic
1 parent 7f9f7b7 commit 15a4f2f

1 file changed

Lines changed: 7 additions & 16 deletions

File tree

Modules/md5module.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ class MD5Type "MD5object *" "&PyType_Type"
3939

4040

4141
typedef struct {
42-
PyObject_HEAD
43-
// Prevents undefined behavior via multiple threads entering the C API.
44-
bool use_mutex;
45-
PyMutex mutex;
42+
HASHLIB_OBJECT_HEAD
4643
Hacl_Hash_MD5_state_t *hash_state;
4744
} MD5object;
4845

@@ -308,30 +305,20 @@ _md5_md5_impl(PyObject *module, PyObject *data, int usedforsecurity,
308305
}
309306

310307
MD5object *new;
311-
Py_buffer buf;
312-
313-
if (string) {
314-
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
315-
}
316-
317308
MD5State *st = md5_get_state(module);
318309
if ((new = newMD5object(st)) == NULL) {
319-
if (string) {
320-
PyBuffer_Release(&buf);
321-
}
322310
return NULL;
323311
}
324312

325313
new->hash_state = Hacl_Hash_MD5_malloc();
326314
if (new->hash_state == NULL) {
327315
Py_DECREF(new);
328-
if (string) {
329-
PyBuffer_Release(&buf);
330-
}
331316
return PyErr_NoMemory();
332317
}
333318

334319
if (string) {
320+
Py_buffer buf;
321+
GET_BUFFER_VIEW_OR_ERROR(string, &buf, goto error);
335322
if (buf.len >= HASHLIB_GIL_MINSIZE) {
336323
/* We do not initialize self->lock here as this is the constructor
337324
* where it is not yet possible to have concurrent access. */
@@ -346,6 +333,10 @@ _md5_md5_impl(PyObject *module, PyObject *data, int usedforsecurity,
346333
}
347334

348335
return (PyObject *)new;
336+
337+
error:
338+
Py_XDECREF(new);
339+
return NULL;
349340
}
350341

351342

0 commit comments

Comments
 (0)