summaryrefslogtreecommitdiff
path: root/py/objstr.c
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2022-08-24 12:22:57 +1000
committerDamien George <damien@micropython.org>2022-08-26 16:45:46 +1000
commit6c3d8d38bfbe24c3d810f89f546ac947cf0cb66d (patch)
treead9cd46fbe8242c0764216a814836d8ddc0983ce /py/objstr.c
parent3a910b15650636efc58bce48cc1bfa0debfd375c (diff)
py/objstr: Always validate utf-8 for mp_obj_new_str.
All uses of this are either tiny strings or not-known-to-be-safe. Update comments for mp_obj_new_str_copy and mp_obj_new_str_of_type. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'py/objstr.c')
-rw-r--r--py/objstr.c11
1 files changed, 6 insertions, 5 deletions
diff --git a/py/objstr.c b/py/objstr.c
index 5a6223751..683d035e4 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -202,11 +202,7 @@ mp_obj_t mp_obj_str_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_
} else {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
- #if MICROPY_PY_BUILTINS_STR_UNICODE_CHECK
- if (!utf8_check(bufinfo.buf, bufinfo.len)) {
- mp_raise_msg(&mp_type_UnicodeError, NULL);
- }
- #endif
+ // This will utf-8 check the input.
return mp_obj_new_str(bufinfo.buf, bufinfo.len);
}
}
@@ -2268,6 +2264,11 @@ mp_obj_t mp_obj_new_bytes_from_vstr(vstr_t *vstr) {
}
mp_obj_t mp_obj_new_str(const char *data, size_t len) {
+ #if MICROPY_PY_BUILTINS_STR_UNICODE && MICROPY_PY_BUILTINS_STR_UNICODE_CHECK
+ if (!utf8_check((byte *)data, len)) {
+ mp_raise_msg(&mp_type_UnicodeError, NULL);
+ }
+ #endif
qstr q = qstr_find_strn(data, len);
if (q != MP_QSTRnull) {
// qstr with this data already exists