@@ -139,7 +139,10 @@ EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
139
139
process = MUNCH_SIZE ;
140
140
else
141
141
process = Py_SAFE_DOWNCAST (len , Py_ssize_t , unsigned int );
142
- EVP_DigestUpdate (self -> ctx , (const void * )cp , process );
142
+ if (!EVP_DigestUpdate (self -> ctx , (const void * )cp , process )) {
143
+ _setException (PyExc_ValueError );
144
+ break ;
145
+ }
143
146
len -= process ;
144
147
cp += process ;
145
148
}
@@ -209,7 +212,10 @@ EVP_digest(EVPobject *self, PyObject *unused)
209
212
return _setException (PyExc_ValueError );
210
213
}
211
214
digest_size = EVP_MD_CTX_size (temp_ctx );
212
- EVP_DigestFinal (temp_ctx , digest , NULL );
215
+ if (!EVP_DigestFinal (temp_ctx , digest , NULL )) {
216
+ _setException (PyExc_ValueError );
217
+ return NULL ;
218
+ }
213
219
214
220
retval = PyBytes_FromStringAndSize ((const char * )digest , digest_size );
215
221
EVP_MD_CTX_free (temp_ctx );
@@ -237,7 +243,10 @@ EVP_hexdigest(EVPobject *self, PyObject *unused)
237
243
return _setException (PyExc_ValueError );
238
244
}
239
245
digest_size = EVP_MD_CTX_size (temp_ctx );
240
- EVP_DigestFinal (temp_ctx , digest , NULL );
246
+ if (!EVP_DigestFinal (temp_ctx , digest , NULL )) {
247
+ _setException (PyExc_ValueError );
248
+ return NULL ;
249
+ }
241
250
242
251
EVP_MD_CTX_free (temp_ctx );
243
252
@@ -362,7 +371,12 @@ EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
362
371
PyBuffer_Release (& view );
363
372
return -1 ;
364
373
}
365
- EVP_DigestInit (self -> ctx , digest );
374
+ if (!EVP_DigestInit (self -> ctx , digest )) {
375
+ _setException (PyExc_ValueError );
376
+ if (data_obj )
377
+ PyBuffer_Release (& view );
378
+ return -1 ;
379
+ }
366
380
367
381
self -> name = name_obj ;
368
382
Py_INCREF (self -> name );
@@ -461,7 +475,11 @@ EVPnew(PyObject *name_obj,
461
475
if (initial_ctx ) {
462
476
EVP_MD_CTX_copy (self -> ctx , initial_ctx );
463
477
} else {
464
- EVP_DigestInit (self -> ctx , digest );
478
+ if (!EVP_DigestInit (self -> ctx , digest )) {
479
+ _setException (PyExc_ValueError );
480
+ Py_DECREF (self );
481
+ return NULL ;
482
+ }
465
483
}
466
484
467
485
if (cp && len ) {
@@ -902,6 +920,8 @@ generate_hash_name_list(void)
902
920
* the generic one passing it a python string and are noticeably
903
921
* faster than calling a python new() wrapper. Thats important for
904
922
* code that wants to make hashes of a bunch of small strings.
923
+ * The first call will lazy-initialize, which reports an exception
924
+ * if initialization fails.
905
925
*/
906
926
#define GEN_CONSTRUCTOR (NAME ) \
907
927
static PyObject * \
@@ -914,6 +934,17 @@ generate_hash_name_list(void)
914
934
if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
915
935
return NULL; \
916
936
} \
937
+ \
938
+ if (CONST_new_ ## NAME ## _ctx_p == NULL) { \
939
+ EVP_MD_CTX *ctx_p = EVP_MD_CTX_new(); \
940
+ if (!EVP_get_digestbyname(#NAME) || \
941
+ !EVP_DigestInit(ctx_p, EVP_get_digestbyname(#NAME))) { \
942
+ _setException(PyExc_ValueError); \
943
+ EVP_MD_CTX_free(ctx_p); \
944
+ return NULL; \
945
+ } \
946
+ CONST_new_ ## NAME ## _ctx_p = ctx_p; \
947
+ } \
917
948
\
918
949
if (data_obj) \
919
950
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \
@@ -942,10 +973,6 @@ generate_hash_name_list(void)
942
973
#define INIT_CONSTRUCTOR_CONSTANTS (NAME ) do { \
943
974
if (CONST_ ## NAME ## _name_obj == NULL) { \
944
975
CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \
945
- if (EVP_get_digestbyname(#NAME)) { \
946
- CONST_new_ ## NAME ## _ctx_p = EVP_MD_CTX_new(); \
947
- EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
948
- } \
949
976
} \
950
977
} while (0);
951
978
0 commit comments