py/binary: mp_binary_get_size: Raise error on unsupported typecodes.
Previouly, we had errors checked in callers, which led to duplicate code
or missing checks in some places.
diff --git a/py/binary.c b/py/binary.c
index d22e0f3..6450478 100644
--- a/py/binary.c
+++ b/py/binary.c
@@ -33,6 +33,7 @@
#include "py/binary.h"
#include "py/smallint.h"
#include "py/objint.h"
+#include "py/runtime.h"
// Helpers to work with binary-encoded data
@@ -100,6 +101,11 @@
}
}
}
+
+ if (size == 0) {
+ mp_raise_ValueError("bad typecode");
+ }
+
if (palign != NULL) {
*palign = align;
}
diff --git a/py/modstruct.c b/py/modstruct.c
index 88411ff..3c99ef1 100644
--- a/py/modstruct.c
+++ b/py/modstruct.c
@@ -113,9 +113,6 @@
} else {
mp_uint_t align;
size_t sz = mp_binary_get_size(fmt_type, *fmt, &align);
- if (sz == 0) {
- mp_raise_ValueError("unsupported format");
- }
while (cnt--) {
// Apply alignment
size = (size + align - 1) & ~(align - 1);
diff --git a/py/objarray.c b/py/objarray.c
index 8e1d32f..ed666df 100644
--- a/py/objarray.c
+++ b/py/objarray.c
@@ -94,9 +94,6 @@
#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
STATIC mp_obj_array_t *array_new(char typecode, mp_uint_t n) {
int typecode_size = mp_binary_get_size('@', typecode, NULL);
- if (typecode_size == 0) {
- mp_raise_msg(&mp_type_ValueError, "bad typecode");
- }
mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
#if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_PY_ARRAY
o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;