Skip to content

Commit 005226a

Browse files
committed
uhashlib: some functions should refuse unicode for python3 compatibility
.. this maybe should be subject to MICROPY_CPYTHON_COMPAT, except that is not defined in the main circuitpython ports so it would be a change that makes no difference.
1 parent b2084d3 commit 005226a

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

extmod/moduhashlib.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@
3636
#include "lib/axtls/crypto/crypto.h"
3737
#endif
3838

39+
static void check_not_unicode(const mp_obj_t arg) {
40+
#if MICROPY_CPYTHON_COMPAT
41+
if (MP_OBJ_IS_STR(arg)) {
42+
mp_raise_TypeError("a bytes-like object is required");
43+
}
44+
#endif
45+
}
46+
3947
typedef struct _mp_obj_hash_t {
4048
mp_obj_base_t base;
4149
char state[0];
@@ -70,6 +78,7 @@ STATIC mp_obj_t sha1_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
7078
#endif
7179

7280
STATIC mp_obj_t hash_update(mp_obj_t self_in, mp_obj_t arg) {
81+
check_not_unicode(arg);
7382
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
7483
mp_buffer_info_t bufinfo;
7584
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
@@ -80,6 +89,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(hash_update_obj, hash_update);
8089

8190
#if MICROPY_PY_UHASHLIB_SHA1
8291
STATIC mp_obj_t sha1_update(mp_obj_t self_in, mp_obj_t arg) {
92+
check_not_unicode(arg);
8393
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
8494
mp_buffer_info_t bufinfo;
8595
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);

0 commit comments

Comments
 (0)